dynamitemedia

Date difference

6 posts in this topic

ok i have found a lot of solutions when using datdiff etc but the issue is the excel file is coming back with dates like this:

Date=Fri, 08 May 2015 

how can i compare that date to today's date?

i am getting today's date using this:

Func _GetTodaysDate($iReturnTime = 1)
    Local $aMDay[8] = [7, "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"], _
            $aMonth[13] = [12, "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"], $aTime[2] = ["", ' ' & @HOUR & ':' & @MIN & ':' & '00']
    Return $aMDay[@WDAY] & ', ' & @MDAY & ' ' & $aMonth[@MON] & ' ' & @YEAR & $aTime[$iReturnTime]
 EndFunc

this will return:
Sun, 10 May 2015

it would be nice to just compare like this:

If $todaysDate < $iniDate then

I have looked at datdiff and stringcompare but that means i have to change the format of dates... which i can do using today's date but cant figure out how to take the 
Date=Fri, 08 May 2015  into a all numeric format

any ideas?

 

Share this post


Link to post
Share on other sites



This should work as long as the Excel date format is the same as the $sDateTimeFormat parameter of the '_Date_Time_Convert' function.

Example: "Date=Fri, 08 May 2015" is in the format of  "Date=ddd, dd MMMM yyyy".

Being May, the month format could be "MMMM" or "MMM".

#include <Date.au3>

Local $sExcelDate = "Date=Fri, 08 May 2015"
Local $sExcelDateConv = _Date_Time_Convert($sExcelDate, "Date=ddd, dd MMMM yyyy", "yyyy/MM/dd")
Local $sNow = _NowCalcDate() ; Returns today's date in "yyyy/MM/dd" format.

ConsoleWrite("Excel  Date  = " & $sExcelDateConv & @LF) ;
ConsoleWrite("Today's Date = " & $sNow & @LF)

ConsoleWrite("Difference = " & _DateDiff("D", $sExcelDateConv, $sNow) & " days" & @LF)


; http://www.autoitscript.com/forum/topic/112520-date-time-convert/#entry788257
; #FUNCTION# ==================================================================================
; Name...........: _Date_Time_Convert
; Description ...: Converts a date and/or time from its existing format to any other custom format.
; Example Date time "Thursday, 1 April, 2010 @ 08:05:08 PM"
; Example's Format Eg : "dddd, d MMMM, yyyy @ hh:mm:ss tt"
; Year. : yyyy = 2010 ; yy = 10
; Month : MMMM = April; MMM = Apr; MM = 04; M = 4
; Day.. : dddd = Thursday; ddd = Thu; dd = 01; d = 1
; Hour. : HH = 20; H = 20 (1 digit minimum); hh = 08; h = 8 (Lowercase h's used with AM/PM time)
; Minute: mm = 05 (2 digit minimum); m = 5 (1 digit minimum)
; Second: ss = 08 (2 digit minimum); s = 8 (1 digit minimum)
; AM/PM : tt = AM or PM; t = A or P
; Ref: https://msdn.microsoft.com/en-us/library/bb761726%28v=VS.85%29.aspx#dtp_format_chars
; =============================================================================================
Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next

    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec

    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc   ;==>_Date_Time_Convert

 

Share this post


Link to post
Share on other sites
 cant figure out how to take the Date=Fri, 08 May 2015  into a all numeric format
 

​This is a simple way to return your date in YYYY/MM/DD format to use with DateDiff()

$mydate = "Date=Fri, 08 May 2015"
Msgbox(0,"", _DateCalc($mydate))

Func _DateCalc($date)
  Local $mon, $tmp = StringSplit($date, " ")
  Local $aMonth[13] = [12, "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
  For $i = 1 to 12
     If $aMonth[$i] = $tmp[3] Then $mon = $i
  Next
  Return StringFormat("%4i/%02i/%02i", $tmp[4], $mon, $tmp[2])
EndFunc

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

How sure are you that you will always get the same date format from Excel (i.e. will your script be running on different machines with different locales, languages, date format preferences etc. or is it just for one machine that you control yourself)? And how are you reading it, direct interaction with Excel or from an exported CSV file or something?

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

thanks guys will mess with it later tonight  actually i missed the $ lol  should be

$date = "Fri, 08 May 2015"

will post some code when i get home

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

OK switched from excel and went to INI...

I also incorporated code from here:  https://www.autoitscript.com/forum/topic/164539-better-way-to-write-a-task-scheduler/

Please tell me  if there is a cleaner way of doing this:

 

runTask()
local $ini = @workingdir & "\config.ini"
local $Tasks,$TaskInfo,$OldIni

while 1
   if FileRead($ini) <> $OldIni then                        ; Check for any updates to the ini file
      Local $sFileRead = FileRead($ini)
      ConsoleWrite( "Ini :  " & $sFileRead  & @CRLF)

      Local $sFileRead = FileRead($OldIni)
      ;ConsoleWrite( "Old INI :  " & $sFileRead  & @CRLF)

      $Tasks = IniReadSectionNames($ini)                    ;Gathers each section name from ini (the identifier used per task in this example)
      $OldIni = FileRead($ini)                              ;Stores Entire contents of Ini to variable (Cheap and ugly update check on the config)

      if IsArray($TaskInfo) then                            ;Avoid ReDim Errors by checking if array exists before declaration
         ReDim $TaskInfo[Ubound($Tasks)][5]
      Else
         Dim $TaskInfo[Ubound($Tasks)][5]
      EndIf

      For $i = 0 to uBound($Tasks) - 1                      ; For Loop to gather all task data for each task into an array
         $TaskInfo[$i][0] = $Tasks[$i]
         $TaskInfo[$i][1] = iniRead($ini,$Tasks[$i],"Location","")
         $TaskInfo[$i][2] = iniRead($ini,$Tasks[$i],"Date","")
         $TaskInfo[$i][3] = iniRead($ini,$Tasks[$i],"Time","")
         $TaskInfo[$i][4] = False                           ;Flag to ensure only run once
      Next
   EndIf

   For $x = 0 to Ubound($TaskInfo,1) - 1                    ;For loop to check if any tasks are to be run.
      Local $todaysDate = _GetTodaysDate(0)
         ;ConsoleWrite( "todaysDate :  " & $todaysDate & @CRLF) ; If you're in SciTE

      Local $iniDate = $TaskInfo[$x][2]
         ;ConsoleWrite( "iniDate :  " & $iniDate & @CRLF) ; If you're in SciTE

      Local $deleteEntry = $TaskInfo[$x][0]
         ;ConsoleWrite( "deleteEntry :  " & $deleteEntry & @CRLF) ; If you're in SciTE

      Local $sTodaysDate = _Date_Time_Convert($iniDate, "ddd, dd MMMM yyyy", "yyyy/MM/dd")
         ;ConsoleWrite( "sTodaysDate :  " & $sTodaysDate & @CRLF) ; If you're in SciTE
      Local $sNow = _Date_Time_Convert($todaysDate, "ddd, dd MMMM yyyy", "yyyy/MM/dd")
         ;ConsoleWrite( "sNow :  " & $sNow & @CRLF) ; If you're in SciTE

      $sDifference = _DateDiff("D", $sNow, $sTodaysDate)
         ;ConsoleWrite( "Days Difference :  " & $sDifference & @CRLF & @CRLF) ; If you're in SciTE

      If $sDifference <= 0 then
         ;ConsoleWrite( "Difference is less than 0, lets delete and is : " & $sDifference  & @CRLF) ; If you're in SciTE
         IniDelete($ini,$deleteEntry)                       ; Now lets delete Entry
      EndIf

      If $sDifference >= 0 then

         If $sDifference == 0 then
            $times = @HOUR & ":" & @MIN
            ;ConsoleWrite( "Difference is less than 0, lets delete and is : " & $sDifference  & @CRLF) ; If you're in SciTE
               if $times = $TaskInfo[$x][3] and $TaskInfo[$x][4] == False Then
                  $task = $TaskInfo[$x][1]
                  ConsoleWrite( "that is today SO lets do something: " & $task  & @CRLF) ; If you're in SciTE

                  Run($task)                                        ; Run the task using the Directory value stored in the config file
                  $TaskInfo[$x][4] = True                       ; Set the run flag to true for this task
                  ;IniDelete($ini,$deleteEntry)                     ; Now lets delete Entry
               EndIf
            EndIf
         EndIf
   Next
WEnd

EndFunc

Func _GetTodaysDate($iReturnTime = 1)
    Local $aMDay[8] = [7, "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"], _
            $aMonth[13] = [12, "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"], $aTime[2] = ["", ' ' & @HOUR & ':' & @MIN & ':' & '00']
    Return $aMDay[@WDAY] & ', ' & @MDAY & ' ' & $aMonth[@MON] & ' ' & @YEAR & $aTime[$iReturnTime]
 EndFunc

Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        ;MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next



;#cs


    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec




    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn

;#ce


EndFunc   ;==>_Date_Time_Convert

 

EDIT: One thing i may add after deleting the ini entry is deleting any empty lines at the top just to tidy up a bit...

Edited by dynamitemedia
tidy up idea

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • ur
      By ur
      In some systems the date is displayed in the Taskbar as
      10-01-2018
      And in some systems it is like, 1/10/2018
      And in some it is, 10-Jan-2018
      And in different formats.
       
      I wrote my code to convert 1/10/2018 to 10-01-2018
      Func TodaysDate() $NewDate = _DateTimeFormat(_NowCalcDate(),1) $Array = StringSplit( $NewDate , ',' ) _ArrayDelete($Array, 0) _ArrayDelete($Array, 0) $Array1 = StringSplit($Array[0],' ') RemoveEmptyArrayLines($Array1) ;Will return the present day's date with format dd-mmm-yyyy ;$Date = StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL) $Date = StringStripWS($Array1[2]&"-"&changeDateformat(StringLeft($Array1[1], 3))&"-"&$Array[1], $STR_STRIPALL) return $Date EndFunc Func RemoveEmptyArrayLines(ByRef $arrLines) $intCount = 1 While $intCount < UBound($arrLines) $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING) If StringLen($arrLines[$intCount])=0 Then _ArrayDelete($arrLines, $intCount) $intCount = $intCount - 1 EndIf $intCount = $intCount + 1 WEnd EndFunc ;To convert mmm to mm format Func changeDateformat($sText) Local $sMsg = StringStripWS($sText, $STR_STRIPALL) Switch $sMsg Case "Jan" $sMsg = "01" Case "Feb" $sMsg = "02" Case "Mar" $sMsg = "03" Case "Apr" $sMsg = "04" Case "May" $sMsg = "05" Case "Jun" $sMsg = "06" Case "Jul" $sMsg = "07" Case "Aug" $sMsg = "08" Case "Sep" $sMsg = "09" Case "Oct" $sMsg = "10" Case "Nov" $sMsg = "11" Case "Dec" $sMsg = "12" EndSwitch return $sMsg EndFunc But again it will work on machines only with 1/10/2018.
      Is there any direct function which will give only in the format 10-01-2018, whatever the system settings is.?
      I tried all the arguments for _DateTimeFormat, but showing machine dependent settings only.
      Google given these two links in AutoIT but these are also for specific formats only,like mine.
       
      Please suggest.
    • kawliga751
      By kawliga751
      I have established how to get tomorrows date however I can't seem to figure out how to format the date into my required format of MM/DD/YYYY.  
      I have this:
      $today=_DateToDayValue(@YEAR,@MON,@MDAY) Dim $Y, $M, $D $tomorrow=_DayValueToDate($today+1, $Y, $M, $D) The _DateToDayValue seems to have a fixed format.  I've also tried a :
      $vardate = _DateAdd( 'd',1, _NowCalcDate()) with a "StringSplit" and/or StringFormat but the _NowCalcDate seems to also be fixed to a YYYY/MM/DD format.
      I'm sure I'm missing something simple but am at a loss.
      Any help would be MUCH appreciated
       
       
    • ur
      By ur
      To get the current time stamp, I got the below code.
      #include <Date.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #RequireAdmin ; Under Vista the Windows API "SetSystemTime" may be rejected due to system security $td = _Date_Time_GetSystemTime() $td = _Date_Time_SystemTimeToDateTimeStr($td) $td = StringReplace($td, " ", "_") $td = StringReplace($td, ":", "_") MsgBox(0,"",$td) But it is not giving the date or time of the timezone where the system is there.
      Please suggestt
    • ur
      By ur
      When I tried the below code, I am getting the date in mm-dd-yyyy format.
      But I require dd-mm-yyyy format.
      Can anyone suggest how to do that.
      Func TodaysDate() return StringReplace(_DateTimeFormat(_NowCalc(), 2), "/", "-") EndFunc Earlier I tried dd-mmm-yyyy format and it is working with below code.
      Func TodaysDate() $NewDate = _DateTimeFormat(_NowCalcDate(),1) $Array = StringSplit( $NewDate , ',' ) _ArrayDelete($Array, 0) _ArrayDelete($Array, 0) $Array1 = StringSplit($Array[0],' ') RemoveEmptyArrayLines($Array1) $Date = StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL) return $Date EndFunc Which is unnecessarily complicated I feel., the approach
      Now I need format of dd-mm-yyyy.
      Can anyone suggest how to do this.
    • MrCheese
      By MrCheese
      Hi guys,
       
      I have some code; which doesn't quite work.
      $dateStop = "11302015" $tDate = _Date_Time_GetSystemTime() $currentdatestring = _Date_Time_SystemTimeToDateStr($tDate) If $currentdatestring >= $dateStop Then MsgBox(0, "Expired", "Script has expired.") Exit EndIf  
      Basically, I want to essentially put an expiry date on the script.
       
      Where in the example above, it is 30th November 2015.
       
      Sometimes it works, sometimes it doesn't. 
      I may have the string wrong, I may have the code all wrong.
       
      I thought about using the macro @mon @year etc but it gets quite conditional.
      i.e. if its 2015, but October, or if its 2015 and Dec, or if its March 2016 etc.
       
      But simplicity is great. Any help would be awesome.