Jump to content

Recommended Posts

it recomended to use the builten function

_DateDiff

 

 

 

hello autoit team
i made a simple function to help me get the difference between to date
i liked to share it with you
the paramatrs of this function
is firstdate and lastedate
the two params is as the following
year/month/day hours:minuts:seconds
here is the function
 

#include <date.au3>
func _dateBetween($s_firstDate, $s_lastDate = default)
if not (StringRegExp($s_firstDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(1, 0, 0)
if $s_lastDate = default then $s_lastDate = @year & "/" & @mon & "/" & @mday & " " & @hour & ":" & @min & ":" & @sec
if not (StringRegExp($s_lastDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(2, 0, 0)

local $a_FirstSplitDate = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$2"), "/", 2)
local $a_FirstSplitTime = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$3"), ":", 2)

local $a_lastSplitDate = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$2"), "/", 2)
local $a_LastSplitTime = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$3"), ":", 2)
local $i_firstDateValue =  _DateToDayValue($a_FirstSplitDate[0], $a_FirstSplitDate[1], $a_FirstSplitDate[2])
local $i_LastDateValue =  _DateToDayValue($a_LastSplitDate[0], $a_LastSplitDate[1], $a_LastSplitDate[2])
if $i_firstDateValue > $i_LastDateValue then Return SetError(3, 0, 0)

local $i_totalDays = $i_LastDateValue-$i_firstDateValue
if not ($i_totalDays = 0) then
if $a_FirstSplitTime[0] <= $a_lastSplitTime[0] then
$i_totalDays += 1
$a_lastSplitTime[0] -= $a_FirstSplitTime[0]
elseIf $a_FirstSplitTime[0] > $a_lastSplitTime[0] then
if $i_totalDays > 0 then 
$i_totalDays -= 1
$a_FirstSplitTime[0] -= $a_lastSplitTime[0]
endIf
endIf
endIf

local $i_totalYears = 0
if $i_totalDays >= 365 then
While $i_totalDays >= 365
$i_totalYears += 1
$i_totalDays -= 365
Wend
endIf

local $i_TotalMonths = 0
if $i_totalDays >= 30 then
While $i_totalDays >= 30
$i_totalMonths += 1
$i_totalDays -= 30
Wend
endIf

local $i_totalWeeks = 0
if $i_totalDays >= 7 then
While $i_totalDays >= 7
$i_totalWeeks += 1
$i_totalDays -= 7
Wend
endIf

$i_totalSeconds = ((($a_FirstSplitTime[0]*60)*60)+($a_FirstSplitTime[1]*60)+($a_FirstSplitTime[2]))-((($a_lastSplitTime[0]*60)*60)+($a_lastSplitTime[1]*60)+($a_LastSplitTime[2]))
local $minus = ""
if $i_totalSeconds  < 0 then 
$minus = "-"
$i_totalSeconds = StringReplace($i_totalSeconds, "-", "")
endIf
local $i_totalHours = 0
if $i_totalSeconds >= 3600 then
While $i_totalSeconds >= 3600
$i_totalHours += 1
$i_totalSeconds -= 3600
Wend
endIf

local $i_totalminuts = 0
if $i_totalSeconds >= 60 then
While $i_totalSeconds >= 60
$i_totalminuts += 1
$i_totalSeconds -= 60
Wend
endIf
local $a_arrayReturn[7]
$a_arrayReturn[0] = $i_totalYears
$a_arrayReturn[1] = $i_totalMonths
$a_arrayReturn[2] = $i_totalWeeks
$a_arrayReturn[3] = $i_totalDays
$a_arrayReturn[4] = $minus & $i_totalHours
$a_arrayReturn[5] = $minus & $i_totalminuts
$a_arrayReturn[6] = $minus & $i_totalSeconds
return $a_arrayReturn
endFunc

 

here is an example

 

 

$a_calc = _dateBetween("2015/02/12 23:00:05", "2030/02/12 23:25:50")
msgBox(64, "result", StringFormat("the difference is : %i years and %i months and %i weeks and %i days and %i hours and %i minuts and %i seconds", $a_calc[0], $a_calc[1], $a_calc[2], $a_calc[3], $a_calc[4], $a_calc[5], $a_calc[6]))

 

am waiting for your comments

with my greetings

Edited by nacerbaaziz

Share this post


Link to post
Share on other sites

Also not all months are 30 days, not all years are 365 days, + what @JLogan3o13 just wrote.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Jahar
      Hi All,
      Please help me on how to fetch date/time of last windows 10 system restore point using autoit? Basically its about querying the last restore point.
    • By TheDcoder
      Hi everyone,
      I am bit stumped as to why I am not able to set the time in the Date control
      #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() GUICreate("My GUI get date", 200, 200, 800, 200) Local $idDate = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20, $DTS_TIMEFORMAT) ; to select a specific default format Local $sStyle = "HH:mm:ss" GUICtrlSendMsg($idDate, $DTM_SETFORMATW, 0, $sStyle) ; Set time Local $iRet = GUICtrlSetData($idDate, '13:33:37') ConsoleWrite('GUICtrlSetData returned ' & ($iRet = 1 ? 'success' : 'failure') & @CRLF) GUISetState(@SW_SHOW) ; Loop until the user exits. While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd MsgBox($MB_SYSTEMMODAL, "Time", GUICtrlRead($idDate)) EndFunc ;==>Example The documentation for GUICtrlSetData clearly mentions that it uses the same format as GUICtrlRead:
      But I get failure
      What gives?
      Thanks for the help in advance!
    • By Jeep
      Here is an other UDF for string handling :
      Date handling
      _StringDateConvert:  convert a date from one format ("YMD", "MDY" or "DMY") to another.
      _StringIsDate:  checks if a date with a given format is valid
      String management
      _StringCount: count of occurrences that appear in a string
      _StringFormatBytesSize: formatting a dimension expressed in bytes (bytes) in MB, TB, ...)
      _StringIsEndingWith: check if a string end with some characters
      _StringIsStartingWith: check if a string start with some characters
      _StringJoinArray:  concatenate elements of an array to rebuild a string
      _StringPadLeft: filling a string with characters on the left
      _StringPadRight: fill a string with characters on the right
      _StringRemoveFrenchAccent: remove french accent
      _StringRemoveChars: deleting characters from a string
      _StringStrip: eliminate characters at the begin and/or at the  end of a string
      _StringTitleCaseFrench: capitalize the first letter of each word with elimination of french accents
      _StringWSClean: simple replacement of "White Spaces",  remove beginning and trailing spaces and multiple spaces removal
      Any comments, suggestions for improvement or constructive criticism are welcome.
      Below you will find the UDF and a demo program. 
       
       
      JPD_String.zip
    • By Skeletor
      Hey All,
      I'm trying to set the date using a variable.
      Basically, I set the date into the input box, then I change the input box, say, I change the year.
      Then I set what I typed into the input box into the Date Picker.

      This is a demo code.
      #include <ButtonConstants.au3> ;Start GUI includes #include <EditConstants.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <GuiDateTimePicker.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 438, 192, 124) $Date1 = GUICtrlCreateDate("2019/02/02 23:16:26", 80, 64, 186, 21, $DTS_SHORTDATEFORMAT) $Input1 = GUICtrlCreateInput("Input1", 80, 152, 185, 21) $Button1 = GUICtrlCreateButton("Set data", 176, 96, 75, 25) $Button2 = GUICtrlCreateButton("Read from input", 176, 200, 75, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $Read = GUICtrlRead($Date1) GUICtrlSetData($Input1, $Read) MsgBox(-1, "", $Read) Case $Button2 $Read = GUICtrlRead($Input1) $New_date = StringReplace($Read, "/", "") $DAY = StringLeft($New_date, 2) $MON = StringMid($New_date, 4, 3) $YEAR = StringRight($New_date, 4) MsgBox(-1, "", $DAY & $MON & $YEAR) ;_GUICtrlDTP_SetFormat($hWndDate, "yyyy/MM/dd") $DateFormate = ($DAY & " " & $MON & " " & $YEAR) $DTM_SETFORMAT_ = 0x1032 ; $DTM_SETFORMATW GUICtrlSendMsg($Date1, $DateFormate, 0, "MM/dd/yyyy") EndSwitch WEnd  
    • By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
×
×
  • Create New...