RickB75 Posted February 1, 2016 Posted February 1, 2016 No... I not asking any of you folks out on a date LOL . I have a need to get the last day of last month if an error occurs. Sounds simple enough right... The format is my issue. #include <Date.au3> #include <String.au3> $LstDay = _DateDaysInMonth(@YEAR,@MON - 1) MsgBox(0,"date", @MON - 1 & "/" & $LstDay & "/" & StringTrimLeft(@YEAR, 2)) MsgBox(0,"date", @MON & "/" & @MDAY & "/" & StringTrimLeft(@YEAR, 2)) I'm trying to get the date for the last day of last month in this format. MM/DD,YY. Currently it's giving me this format M/DD/YY. How can I do this?
AutoBert Posted February 1, 2016 Posted February 1, 2016 (edited) Using StringFormat will help Edited February 1, 2016 by AutoBert
RickB75 Posted February 1, 2016 Author Posted February 1, 2016 Thanks for your reply Bert. Very quick reply I may add as well. I have the the macros in the correct order. The problem is when I subtract 1 from the current month, it gives me a single digit instead of a double digit month. It goes from this 02/01/16 to this 1/31/16. I need the zero in front of the month. I'm sure I could add a piece in and check for the size of the date and if the @mon is less than two digits, add a zero in front of it. I was just looking to see if there was a diff approach.
AutoBert Posted February 1, 2016 Posted February 1, 2016 (edited) Yes i see meanwhile, but you where quicker in repconce than i with editing. (eating) #include <Date.au3> #include <String.au3> $LstDay = _DateDaysInMonth(@YEAR,@MON - 1) MsgBox(0,"date", StringFormat('%02s/%02s/%02s',@MON,$LstDay,StringTrimLeft(@YEAR, 2))) MsgBox(0,"date", StringFormat('%02s/%02s/%02s',@MON,@MDAY,StringTrimLeft(@YEAR, 2))) Edited February 1, 2016 by AutoBert
Malkey Posted February 1, 2016 Posted February 1, 2016 Try this example. #include <Date.au3> Local $LstDay = _DateAdd("D", -1, @YEAR & '/' & @MON & "/01") ; Return format (YYYY/MM/DD) ConsoleWrite($LstDay & @LF) MsgBox(0, "Date (MM/DD/YY)", StringRegExpReplace($LstDay, "(\d{2})(\d{2})/(\d{2})/(\d{2})", "\3/\4/\2")) MsgBox(0, "Date (DD/MM/YY)", StringRegExpReplace($LstDay, "(\d{2})(\d{2})/(\d{2})/(\d{2})", "\4/\3/\2")) ;.......................................Back references = ..^ \1....^ \2....^ \3....^ \4.....^ Rearrange output order of back-references. ; Each set of brackets "()" define back-references, numbered left to right.
RickB75 Posted February 1, 2016 Author Posted February 1, 2016 Bert... Your solution is perfect! I need to read up on the help file page for StringFormat to understand how it's working and and get a better understanding of the function.
RickB75 Posted February 1, 2016 Author Posted February 1, 2016 Malkey, your's is working to!! I just tested it. I have a hard time with those regex's though.
Malkey Posted February 2, 2016 Posted February 2, 2016 (edited) I believe there is a problem with AutoBert's example when the current month is January:- "@MON - 1 = 0", zero (0) is outside the $iMonthNum parameter of the _DateDaysInMonth () function; and, The year would be "@YEAR - 1", because the previous month of January is also the previous year. If regular expressions are not to your liking, maybe other string functions are more suitable. #include <Date.au3> Local $LstDay = _DateAdd("D", -1, @YEAR & '/' & @MON & "/01") ; Return format (YYYY/MM/DD) ;ConsoleWrite($LstDay & @LF) MsgBox(0, "Date (MM/DD/YY)", StringMid($LstDay, 6, 3) & StringRight($LstDay, 2) & "/" & StringMid($LstDay, 3, 2)) MsgBox(0, "Date (DD/MM/YY)", StringRight($LstDay, 2) & "/" & StringMid($LstDay, 6, 3) & StringMid($LstDay, 3, 2)) Edited February 2, 2016 by Malkey
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