Jump to content

Date question


RickB75
 Share

Recommended Posts

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?

 

Link to comment
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.

Link to comment
Share on other sites

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
Link to comment
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.

 

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...