Jump to content

Date calculation help needed


Recommended Posts

Hello I can not figure out how to do a simple date calculation. There has to be also two ClipGet in a row to get these two dates so I cant figure how to make them work.

First I have to copy two dates from a program and then calculate if $date_install is made between $date_begin and $date_begin + years.

For example 

$date_begin = 25.5.2019

$date_install = 14.4.2021

$date_being + years is 25.5.2021 and 14.4.201 is in the time limit so this would produce the MsgBox and Consolewrite as in the code...

;copy date of beginning
                     MouseClick($MOUSE_CLICK_PRIMARY, 340, 104, 2)
                     MouseClick("right",340, 104, 1)
                     Sleep(200)
                     Send("{DOWN 3}")
                     Sleep(200)
                     Send("{ENTER}")
                     Sleep(200)
                     $date_begin = ClipGet()
                     
                     ;copy date of install
                     MouseClick($MOUSE_CLICK_PRIMARY, 340, 104, 2)
                     MouseClick("right",340, 225, 1)
                     Sleep(200)
                     Send("{DOWN 3}")
                     Sleep(200)
                     Send("{ENTER}")
                     Sleep(200)
                     $date_install = ClipGet()  

                      
                  If ;calculate if date of install is made between $date_begin + 2 years Then
                     ConsoleWrite("Install made in two years after beginning"&@CRLF)
                     MsgBox(48, 'Error', 'Handling cancelled. Install made in two years after beginning')
                     Exit
                  EndIf

 

Link to comment
Share on other sites

Use _DateDiff() or _DateAdd() but use the expected date format, e.g. "2021/06/17". Help file is your best friend.

BTW using the mouse and Send() is not reliable. Use Control functions.

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)

Link to comment
Share on other sites

How can I use _DateAdd() if my date format is DD/MM/YYYY ? I have got this far already but _DateAdd does not work... I think it is because date format DD/MM/YYYY

MouseClick("left", 340, 104, 2)
                     MouseClick("right",340, 104, 1)
                     Sleep(200)
                     Send("{DOWN 3}")
                     Sleep(200)
                     Send("{ENTER}")
                     Sleep(200)
                     $warranty_start = ClipGet()
                     ConsoleWrite("Begin " & $warranty_start)
                     ClipPut("")

                     $warranty_end = _DateAdd('d', 729, $warranty_start)

                     ConsoleWrite("End " & $warranty_end)
                     ;copy date of install
                     MouseClick("left", 340, 104, 2)
                     MouseClick("right",340, 225, 1)
                     Sleep(200)
                     Send("{DOWN 3}")
                     Sleep(200)
                     Send("{ENTER}")
                     Sleep(200)
                     $date_install = ClipGet()
                     ConsoleWrite("Install date " & $date_install)

                     MsgBox(0, "Check if between", _dateIsBetween($date_install, $warranty_start, $warranty_end))

                     Func _dateIsBetween($sDate0, $sDate1, $sDate2)
                     Return($sDate0 >= $sDate1) And ($sDate0 <= $sDate2)
                     EndFunc   ;==>_dateIsBetween

                     Exit

 

Link to comment
Share on other sites

5 minutes ago, USSSR said:

How can I use _DateAdd() if my date format is DD/MM/YYYY ? I have got this far already but _DateAdd does not work... I think it is because date format DD/MM/YYYY

A simple search with e.g. Google on the topic "Autoit Date Format" delivers many results. Example : date-format-change

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Moderators

USSSR,

Or you could use my DateTimeConvert UDF to get the required format - the link is in my sig.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks folks. I found that the issues is in the date format... as _DateAdd needs it in certain format YYYY/MM/DD right? For example my input date format can be 3.3.2020 which is 3rd of March 2020. But it can also be like 13.10.2020 which is 13th of October 2020.

This leads to a 1st challenge: How to use StringRegExpReplace?

StringRegExpReplace($warranty_start, "(\d{1}).(\d{1}).(\d{4})", " ${3}/${2}/${1} ")

Works for 3.3.2020

StringRegExpReplace($warranty_start, "(\d{2}).(\d{2}).(\d{4})", " ${3}/${2}/${1} ")

Works for 13.10.2020

But how to do both ways depending on the date?

 

Or is the only option to use DateTimeConvert UDF ?

 

 

Link to comment
Share on other sites

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $g_sStartDate = _FormatDate('25/05/2019') ; dd/mm/yyyy OR dd.mm.yyyy
Global $g_sEndDate = _DateAdd('y', 2, $g_sStartDate)
Global $g_sInstallDate = _FormatDate('14/04/2021') ; dd/mm/yyyy OR dd.mm.yyyy

If $g_sInstallDate > $g_sStartDate And _
   $g_sInstallDate < $g_sEndDate Then
    MsgBox(($MB_OK + $MB_ICONINFORMATION), @ScriptName, 'Program was installed within two years of ' & $g_sStartDate)
Else
    MsgBox(($MB_OK + $MB_ICONWARNING), @ScriptName, 'Program WAS NOT installed within two years of ' & $g_sStartDate)
EndIf

Func _FormatDate($sDate)
    Local $aDate
    $aDate = StringSplit($sDate, '/', $STR_NOCOUNT)
    If @ERROR Then
        $aDate = StringSplit($sDate, '.', $STR_NOCOUNT)
        If @ERROR Then
            Return -1
        EndIf
    EndIf
    If UBound($aDate) = 3 Then
        Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])
    EndIf
    Return -1
EndFunc

 

Link to comment
Share on other sites

28 minutes ago, Luke94 said:
#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $g_sStartDate = _FormatDate('25/05/2019') ; dd/mm/yyyy OR dd.mm.yyyy
Global $g_sEndDate = _DateAdd('y', 2, $g_sStartDate)
Global $g_sInstallDate = _FormatDate('14/04/2021') ; dd/mm/yyyy OR dd.mm.yyyy

If $g_sInstallDate > $g_sStartDate And _
   $g_sInstallDate < $g_sEndDate Then
    MsgBox(($MB_OK + $MB_ICONINFORMATION), @ScriptName, 'Program was installed within two years of ' & $g_sStartDate)
Else
    MsgBox(($MB_OK + $MB_ICONWARNING), @ScriptName, 'Program WAS NOT installed within two years of ' & $g_sStartDate)
EndIf

Func _FormatDate($sDate)
    Local $aDate
    $aDate = StringSplit($sDate, '/', $STR_NOCOUNT)
    If @ERROR Then
        $aDate = StringSplit($sDate, '.', $STR_NOCOUNT)
        If @ERROR Then
            Return -1
        EndIf
    EndIf
    If UBound($aDate) = 3 Then
        Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])
    EndIf
    Return -1
EndFunc

 

Are you sure this works if date is 3.3.2020 instead of 03.03.2020 ? I Couldnt get it working..

Link to comment
Share on other sites

7 minutes ago, USSSR said:

Are you sure this works if date is 3.3.2020 instead of 03.03.2020 ? I Couldnt get it working..

Yep, works for me.

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $g_sStartDate = _FormatDate('25.5.2019') ; dd/mm/yyyy OR dd.mm.yyyy
Global $g_sEndDate = _DateAdd('y', 2, $g_sStartDate)
Global $g_sInstallDate = _FormatDate('3.3.2021') ; dd/mm/yyyy OR dd.mm.yyyy

If $g_sInstallDate > $g_sStartDate And _
   $g_sInstallDate < $g_sEndDate Then
    MsgBox(($MB_OK + $MB_ICONINFORMATION), @ScriptName, 'Program was installed within two years of ' & $g_sStartDate)
Else
    MsgBox(($MB_OK + $MB_ICONWARNING), @ScriptName, 'Program WAS NOT installed within two years of ' & $g_sStartDate)
EndIf

Func _FormatDate($sDate)
    Local $aDate
    $aDate = StringSplit($sDate, '/', $STR_NOCOUNT)
    If @ERROR Then
        $aDate = StringSplit($sDate, '.', $STR_NOCOUNT)
        If @ERROR Then
            Return -1
        EndIf
    EndIf
    If UBound($aDate) = 3 Then
        Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])
    EndIf
    Return -1
EndFunc

The leading zeros are added here:

Return StringFormat('%04s/%02s/%02s', $aDate[2], $aDate[1], $aDate[0])

 

Link to comment
Share on other sites

Another skin for this cat:

Local $aDates = ["3.3.2019", "03.03.2019", "3-03/2019", "03 3 2019", "19.6.2019", "19/06/2019", "1.3.2020"]
Local $sToday = _NowCalcDate()
Local $sExpiry, $aDate

For $sDate In $aDates
    $aDate = StringRegExp($sDate, "(\d+)[.\/ -](\d+)[.\/ -](\d+)", 3)
    $sDate = StringFormat("%s/%02s/%02s", $adate[2], $adate[1], $adate[0])
    $sExpiry = _DateAdd("Y", 2, $sDate)
    If $sExpiry < $sToday Then
        ConsoleWrite("Sorry your installation dated " & $sDate & " expired on " & $sExpiry & @LF)
    Else
        ConsoleWrite("Your installation dated " & $sDate & " will expire on " & $sExpiry & ". You have " & _DateDiff("D", $sToday, $sExpiry) & " days left." & @LF)
    EndIf
Next

 

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...