Android Question Date Parsing error

Arun

Member
Licensed User
Longtime User
I am attempting to find the period in minutes between 2 dates.

I was referring to the solution at https://www.b4x.com/android/forum/threads/calculate-difference-between-two-date-strings.39651/ but it does not work for me.

Any help is appreciated.

This is my code

B4X:
    Dim strDateTime1 As String 'DateTime string 201507212215 - yyyyMMddHHmm format
    Dim lngDateTime1 As Long
    Dim strDateTime2 As String 'DateTime string 201507271355 - yyyyMMddHHmm format
    Dim lngDateTime2 As Long
    Dim lPeriod As Period

    ' Calculate the period (in minutes) between 2 dates
    ' My realtime app will have a diff of minutes, or a few days - I need it to be accurate upto the minute

    strDateTime1 = "201507212215"
    strDateTime2 = "201507271355"

    DateTime.DateFormat = "yyyyMMddHHmm"
'    DateTime.TimeFormat = "yyyyMMddHHmm"
' Error on following line: Unparseable date (at offset 12) .....
    lngDateTime1 = DateTime.DateTimeParse(strDateTime1, strDateTime1)
    lngDateTime2 =  DateTime.DateTimeParse(strDateTime2, strDateTime2)
    Log(lngDateTime1)
    Log(lngDateTime2)
    lPeriod=DateUtils.PeriodBetween(lngDateTime1, lngDateTime2)
    Log(lPeriod.Years)
    Log(lPeriod.Months)
    Log(lPeriod.Days)
    Log(lPeriod.Hours)
    Log(lPeriod.Minutes)
    Log(lPeriod.Seconds)

Thanks in advance!

Edit: Changed accuracy from seconds to minutes (it was originally seconds, but after all these problems, if I get diff in minutes, it will do.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim strDateTime1 As String     'DateTime string 201507212215 - yyyyMMddHHmm format
  Dim lngDateTime1 As Long
  Dim strDateTime2 As String 'DateTime string 201507271355 - yyyyMMddHHmm format
  Dim lngDateTime2 As Long
  Dim lPeriod As Period

  ' Calculate the period (in seconds) between 2 dates
  ' My realtime app will have a diff of seconds, or a few days - I need it to be accurate upto the second

  strDateTime1 = "201507212215"
  strDateTime2 = "201507271355"

  DateTime.DateFormat = "yyyyMMdd"
  DateTime.TimeFormat = "HHmm"
  lngDateTime1 = DateTime.DateTimeParse(strDateTime1.SubString2(0,8), strDateTime1.SubString(8))
  lngDateTime2 =  DateTime.DateTimeParse(strDateTime2.SubString2(0,8), strDateTime2.SubString(8))
  Log(lngDateTime1)
  Log(lngDateTime2)
  lPeriod=DateUtils.PeriodBetween(lngDateTime1, lngDateTime2)
  Log(lPeriod.Years)
  Log(lPeriod.Months)
  Log(lPeriod.Days)
  Log(lPeriod.Hours)
  Log(lPeriod.Minutes)
  Log(lPeriod.Seconds)
 
Upvote 0

Arun

Member
Licensed User
Longtime User
B4X:
    Dim strDateTime1 As String     'DateTime string 201507212215 - yyyyMMddHHmm format
  Dim lngDateTime1 As Long
  Dim strDateTime2 As String 'DateTime string 201507271355 - yyyyMMddHHmm format
  Dim lngDateTime2 As Long
  Dim lPeriod As Period

  ' Calculate the period (in seconds) between 2 dates
  ' My realtime app will have a diff of seconds, or a few days - I need it to be accurate upto the second

  strDateTime1 = "201507212215"
  strDateTime2 = "201507271355"

  DateTime.DateFormat = "yyyyMMdd"
  DateTime.TimeFormat = "HHmm"
  lngDateTime1 = DateTime.DateTimeParse(strDateTime1.SubString2(0,8), strDateTime1.SubString(8))
  lngDateTime2 =  DateTime.DateTimeParse(strDateTime2.SubString2(0,8), strDateTime2.SubString(8))
  Log(lngDateTime1)
  Log(lngDateTime2)
  lPeriod=DateUtils.PeriodBetween(lngDateTime1, lngDateTime2)
  Log(lPeriod.Years)
  Log(lPeriod.Months)
  Log(lPeriod.Days)
  Log(lPeriod.Hours)
  Log(lPeriod.Minutes)
  Log(lPeriod.Seconds)

Thanks a lot - This worked great!
 
Upvote 0
Top