Announcement

Collapse
No announcement yet.

Stuck at comparing date time.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Stuck at comparing date time.

    //SOLVED

    Hello guys,

    I'm stuck at comparing after my api change the date from 2019/11/12 to 2191112

    I'm getting errors that it can't compare..




    How can i solve this.
    This is what i had and need:




    Dim StartDate As Date = obj.Item("MYDEVICEDATE").ToString 'Start Date
    Dim CompareDate As Date = Now.Date 'Todays Date
    Dim result as integer = Datetime.Compare(startdate, Comparedate)
    JSON Exception: Conversion from string "20191112" to type 'Date' is not valid.


    So then i tried this just to see if it works :

    Sub Main(ByVal Parms As Object)

    dim timr2 = "20191112"


    Dim StartDate As String = timr2
    Dim CompareDate As string = DateTime.Now.ToString("yyyyMMdd") 'Todays Date

    Dim result as integer = Datetime.Compare(startdate, Comparedate)

    hs.writelog ("look",result)

    end sub
    Error 1 Running script C:\Program Files (x86)\HomeSeer HS3\scripts\10101010.vb :Exception has been thrown by the target of an invocation.Conversion from string "20191112" to type 'Date' is not valid.

    Regards
    Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
    Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




    HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

    #2
    Code:
    Sub Main(ByVal Parms As Object)
    
        Dim timr2 As String = obj.Item("MYDEVICEDATE").ToString
        Dim StartDate As Date = Date.ParseExact(timr2, "yyyyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo)
        Dim CompareDate As Date = DateTime.Now
        Dim result as integer = Datetime.Compare(StartDate, CompareDate)
        hs.writelog ("look",result)
    
    End Sub
    Jon

    Comment


      #3
      One other problem I see that can muck up your compare: You are comparing the timr2 date, which has no hours or minutes specified, with "now()" which does. I suspect Date() would work better (as used in the OP)

      Comment


        #4
        Hi jon,

        thanks for the reply,

        i did try the code but and change it to this, it works but i get the wrong result

        Dim StartDate As String = obj.Item("MYDATE").ToString
        Dim StartDate2 As Date = Date.ParseExact(StartDate, "yyyyMMdd",System.Globalization.DateTimeFormatInfo.Invar iant Info)
        Dim CompareDate As Date = Date.today
        hs.writelog ("comparedate",comparedate)
        hs.writelog ("startdate2",Startdate2)
        Dim result as integer = Datetime.Compare(StartDate2, CompareDate)

        I get result = 1, i dont know why


        the inputdate = 20190519
        and date today = 20190218

        Originally posted by jon00 View Post
        Code:
        Sub Main(ByVal Parms As Object)
        
        Dim timr2 As String = obj.Item("MYDEVICEDATE").ToString
        Dim StartDate As Date = Date.ParseExact(timr2, "yyyyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo)
        Dim CompareDate As Date = DateTime.Now
        Dim result as integer = Datetime.Compare(StartDate, CompareDate)
        hs.writelog ("look",result)
        
        End Sub
        Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
        Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




        HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

        Comment


          #5
          Change the second to last line to:

          Code:
          Dim Result As Integer = (StartDate - CompareDate).Days
          Jon

          Comment


            #6
            Originally posted by jon00 View Post
            Change the second to last line to:

            Code:
            Dim Result As Integer = (StartDate - CompareDate).Days
            thanks again for your help, that did do it
            Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
            Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




            HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

            Comment

            Working...
            X