smud Posted October 12, 2018 Posted October 12, 2018 (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 October 12, 2018 by smud
xcaliber13 Posted October 12, 2018 Posted October 12, 2018 use this to format your date $nowDate = StringRegExpReplace(_DateAdd("d", 0, _NowCalcDate()), '\d\d(\d\d).(\d\d).(\d\d)', '$2/$3/$1') ; Format = MM/DD/YY
mikell Posted October 12, 2018 Posted October 12, 2018 (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 October 12, 2018 by mikell FrancescoDiMuro 1
xcaliber13 Posted October 12, 2018 Posted October 12, 2018 oops you need this $nowDate = StringFormat("%02u/%02u/", @MON, @MDAY) & StringRight(@Year, 4) ; Format = MM/DD/YYYY
Malkey Posted October 13, 2018 Posted October 13, 2018 (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 October 17, 2018 by Malkey Upgraded RE Pattern - See a couple of posts down. Gianni and iamtheky 2
iamtheky Posted October 17, 2018 Posted October 17, 2018 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 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted October 17, 2018 Posted October 17, 2018 @Malkey Nice catch But as \b is an assertion, the lookaround are useless and this works as well StringRegExpReplace($aDate[$i], "\b(\d)\b", "0$1")
Malkey Posted October 17, 2018 Posted October 17, 2018 @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. mikell 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now