Jump to content

Recommended Posts

Posted (edited)

I'm working on a script to compare 2 dates. Checks if the first date is before today's date.

The problem is, dates between the days of 1 and 9 display as example: 10/9/2018.

So when I have today's date as 10/12/2018, the script doesn't work correctly. I tried changing the first date manually to 10/09/2018 and it works properly.

So, how can I format the dates to all display 2 integers for the Day? Ex: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10

#include <Date.au3>

$nowDate = _NowDate()

Local Const $sFilePath = "JustDoThis.ini"
Local $sRead = IniRead($sFilePath, "General", "Date", "Default Value")

If $sRead == $nowDate Then

    ConsoleWrite("Date is Today")

ElseIf $sRead < $nowDate Then

    ConsoleWrite("Date is before today")

EndIf

Thanks!

Edited by smud
Posted (edited)

You might use something like this, to be sure that month and day are always 2 digits long

$date = "10/9/2018"
$tmp = StringSplit($date, "/")
$date = StringFormat("%02i/%02i", $tmp[1], $tmp[2]) & "/"& $tmp[3]
Msgbox(0,"", $date)

Edit :
The same, using a regex  :)

$date = "10/9/2018"
$date = Execute("'" & StringRegExpReplace($date, "^(\d+)/(\d+)", "' & StringFormat('%02i', '$1') & '/' & StringFormat('%02i', '$2') & '") & "'")
Msgbox(0,"", $date)

 

Edited by mikell
Posted (edited)

This changes a single digit "n", into a double digit "0n", when the single digit is in any date format.

Local $aDate[] = ["2010/9/1", "2010/9/18", "10/9/2018", "1/9/2018", "1/09/2018", "1-9-2018", "01.9.2018", "01 9 2018"]

For $i = 0 To UBound($aDate) - 1
    ;$date = StringRegExpReplace($aDate[$i], "(?<=\b)(\d)(?=\b)", "0${1}") ; The RE pattern captures a single digit that is between "word" boundaries.
    ; See mikell's post: https://www.autoitscript.com/forum/topic/196083-date-day-displays-1-integer-instead-of-2/?do=findComment&comment=1406276
    $date = StringRegExpReplace($aDate[$i], "\b(\d)\b", "0${1}") ; The RE pattern captures a single digit that is between "word" boundaries.
    ConsoleWrite($aDate[$i] & @TAB & " = " & $date & @CRLF)
Next

#cs ; Returns:-
    2010/9/1     = 2010/09/01
    2010/9/18    = 2010/09/18
    10/9/2018    = 10/09/2018
    1/9/2018     = 01/09/2018
    1/09/2018    = 01/09/2018
    1-9-2018     = 01-09-2018
    01.9.2018    = 01.09.2018
    01 9 2018    = 01 09 2018
#ce

 

Edited by Malkey
Upgraded RE Pattern - See a couple of posts down.
Posted

can also fix nowcalcdate with stringmid, and then also dont forget the macros; they tend to prove useful.

$nowDate = stringtrimleft(_NowCalcDate() , 5) & stringmid(_NowCalcDate() , 5 , 1) & stringleft(_NowCalcDate() , 4)

$nowDate = @MON & "/" & @MDAY & "/" & @YEAR

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted

@mikell
Because "(?<=X)", "(?=X), and "\b" are all assertions, I would says a look-around a look-around is more redundant than useless - merely semantics.

In development, I went from, 
 "(?<=/)(\d)(?=/)"
to
 "(?<=^|/)(\d)(?=/|$)"
to
 "(?<=\b)(\d)(?=\b)".
I felt replete, and never considered the merest possibility of another step,
 "\b(\d)\b".
Nice catch. :)
Simplicity itself.

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.
×
×
  • Create New...