I am importing .csv files into SQLite and need to convert dates in the format dd-MMM-yyyy into Excel dates.
I have a simple Sub for this:
All works fine, except when the month is September, eg: 27-Sep-1933
In that case there would be an error: Unparsable date, caught by the Try Catch.
Any date not containing Sep is fine.
Any idea why this happens and how I can avoid it.
I can think of several work-arounds, but I would like to understand this first.
RBS
I have a simple Sub for this:
B4X:
Public lTicksOff As Long 'this is set in various Subs, eg InitApp: lTicksOff = DateUtils.SetDate(1970, 1, 1)
Public ExcelJulianDiff As Long = 25569 'days between Jan 01 1900 and Jan 01 1970
Public DaySecs As Long = 86400 'number of seconds in a day
Public DayMiliSecs As Long = 86400000 'number of milli-seconds in a day
'need to make sure to have: DateTime.DateFormat = "dd-MMM-yyyy"
Public Sub EmisDate2XLDate(strEmisDate As String) As Int
Dim lTicks As Long
'for example: "15-Jun-2021"
If strEmisDate.Length <> 11 Then
Return(-1)
End If
'why this fails with Sep?? eg: 27-Sep-1933
'-----------------------------------------
Try
lTicks = DateTime.DateParse(strEmisDate)
Catch
Return(-1)
End Try
Return ExcelJulianDiff + ((lTicks - lTicksOff) / DayMiliSecs)
End Sub
All works fine, except when the month is September, eg: 27-Sep-1933
In that case there would be an error: Unparsable date, caught by the Try Catch.
Any date not containing Sep is fine.
Any idea why this happens and how I can avoid it.
I can think of several work-arounds, but I would like to understand this first.
RBS