DATESERIAL

Returns the date serial number given a date in text format.


Remarks

* For instructions on how to add a function to a workbook refer to the page under Inserting Functions
* The equivalent JavaScript function is DATESERIAL


Public Function DATESERIAL(ByVal sTheDate As String, _ 
         ByVal sDateFormat As String) As Variant

Dim arcomponents As Variant
Dim iyear As Integer
Dim imonth As Integer
Dim iday As Integer

   If (InStr(sTheDate, "/") > 0) Then
      arcomponents = Split(sTheDate, "/")
      If (Len(arcomponents(0)) = 1) Then arcomponents(0) = "0" & arcomponents(0)
      If (Len(arcomponents(1)) = 1) Then arcomponents(1) = "0" & arcomponents(1)
      sTheDate = arcomponents(0) & "/" & arcomponents(1) & "/" & arcomponents(2)
      
      Select Case sDateFormat
         Case "dd/mm/yyyy":
            iyear = Mid(sTheDate, 7, 4)
            imonth = Mid(sTheDate, 4, 2)
            iday = Left(sTheDate, 2)
      
         Case "mm/dd/yyyy":
            iyear = Mid(sTheDate, 7, 4)
            imonth = Left(sTheDate, 2)
            iday = Mid(sTheDate, 4, 2)
            
         Case Default:
            DATESERIAL = "format not found"
            Exit Function
            
      End Select
   End If
                 
   DATESERIAL = VBA.DateSerial(iyear, imonth, iday)
End Function



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext