Sign in to follow this  
Followers 0
RickB75

Date question

8 posts in this topic

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?

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Using

StringFormat

will help

Edited by AutoBert

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by AutoBert

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

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. 

 

 

Share this post


Link to post
Share on other sites

Malkey, your's is working to!! I just tested it. I have a hard time with those regex's though. 

Share this post


Link to post
Share on other sites

#8 ·  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

Share this post


Link to post
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
Sign in to follow this  
Followers 0