Android Question Date Parsing error

Discussion in 'Android Questions' started by Arun, Jul 27, 2015.

  1. Arun

    Arun Member Licensed User

    I am attempting to find the period in minutes between 2 dates.

    I was referring to the solution at http://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

    Code:
    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: Jul 27, 2015
  2. DonManfred

    DonManfred Expert Licensed User

    Code:
    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)
     
    Arun and lemonisdead like this.
  3. Arun

    Arun Member Licensed User

    Thanks a lot - This worked great!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice