Jump to content

Recommended Posts

Posted

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?

 

Posted

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.

Posted

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.

 

Posted

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. 

 

 

Posted (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 by Malkey

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...