FarFigNewton
FarFigNewton

Reputation: 7273

Change format of existing date

I am trying to format a date to look like this 2011-09-19

If I do the following :

leaveDate = "09/19/11"
FormatTime, fileDate, leaveDate, yyyy-MM-dd
MsgBox, %fileDate%

It will just pick up the current date and time because my input date format is invalid. How can I take this 09/19/11 and turn it into this 2011-09-19?

I have read the docs, and I can't find a function to do this. Am I just overlooking it?

Upvotes: 0

Views: 2631

Answers (1)

Gary Hughes
Gary Hughes

Reputation: 4510

Your date needs to be supplied in YYYYMMDDHH24MISS format. If your date format is consistent and all years are after 2000 then the following code using SubStr() to split up the date should work:

leaveDate := "09/19/11"

year := "20" . SubStr(leaveDate, 8, 2)
month := SubStr(leaveDate, 2, 2)
date := SubStr(leaveDate, 5, 2)
formattedTime := year . month . date

FormatTime, fileDate, %formattedTime%, yyyy-MM-dd
MsgBox, %fileDate%

At the bottom of the documentation page for FormatTime a link is given to a forum topic giving the code for a function titled DateParse. This function will take a date in pretty much any format and return a YYYYMMDDHH24MISS formatted string that can be used instead of SubStr() generated string.

The example given is:

DateParse("2:35 PM, 27 November 2007") 

Upvotes: 1

Related Questions