Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Date parser - convert any date format to YYYYMMDDHH24MISS


  • Please log in to reply
94 replies to this topic
JDP
  • Guests
  • Last active:
  • Joined: --
if date is 1/2/09 you need to decide if that should be 1909 or 2009.

here is some code that addresses that:
DateParse(str) {
	static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
	str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
		d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8
	Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t)
		RegExMatch(str, "i)(\d{1,2})\s*:\s*(\d{1,2})(?:\s*(\d{1,2}))?(?:\s*([ap]m))?", t)
			, RegExMatch(str, e2, d)
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	;deal with wrap around of century based on past-future and offset to allow future
	futureYearsAllowed=10
	FormatTime, thisYear,,yyyy
	StringLeft,thisCen,thisYear,2
	lastCen:=thisCen-1
	StringRight,thisYr,thisYear,2
	thisYr+=futureYearsAllowed

	d := (d3 ? (StrLen(d3) = 2 ? (d3>thisYr ? lastCen:thisCen) : "") . d3 : A_YYYY)
		. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
			? d2 + 0.0 : A_MM). ((d1 += 0.0) ? d1 : A_DD) . t1
			+ (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0
	SetFormat, Float, %f%
	Return, d
}


EdScriptNewbie
  • Members
  • 117 posts
  • Last active: Aug 19 2013 07:32 PM
  • Joined: 20 Jan 2007
i'm working to understand time stuff; what is the objective of the previous two posts? THanks
...Ed

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

i'm working to understand time stuff; what is the objective of the previous two posts? THanks

empyrean5 posted my code for supporting American date formats whereas JDP's modification is a smarter algorithm to detect the millennia for a two digit year such as 08 (2008). If you do not need either of these features the script on my first post should suffice for regular use.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


EdScriptNewbie
  • Members
  • 117 posts
  • Last active: Aug 19 2013 07:32 PM
  • Joined: 20 Jan 2007
thanks! ...Ed
...Ed

pleasure2code
  • Guests
  • Last active:
  • Joined: --
Below transformation seems not to work
time := DateParse("2009-Feb-20 11:23:19")
Any suggestions?

Big Digger
  • Members
  • 46 posts
  • Last active: Jul 23 2017 04:06 PM
  • Joined: 07 Feb 2009
DateParse("Sat, 07 Mar 2009 13:43:58 GMT")
DateParse("07 Mar 2009 13:43:58")
DateParse("07 Mar 2009 13:43")
returns for me 200903072543

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006

For such a trivial mod users can refer to the info on this thread. Incorporating it to my official release breaks the simplicity of a single parameter and introduces culture variation dependence which can be complicated given the extra parsing rules, word translations, etc.

Ya, but if you could put it on the first post, you could save a simpleton like me a few pages of reading!! 8) :wink: :cry: :D :p

animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008
I just found your DateParse function, and it works great, thanks for it. I found some scenarios that I want some input on.

Edit:
I forgot to mention that I ran these scenarios on April 12th, 2009. This should clear up confusion when viewed on a later date.

Scenario 1
Date := "January 2nd, 2009"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd, yyyy

;outputs "April 12, 2009"
MsgBox, % FormattedTime

Scenario 2
Date := "January 08, 2009 9:05:27"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd, yyyy h:mm:ss

;outputs "January 08, 2009 9:05:00"
MsgBox, % FormattedTime

Scenario 3
In this case, I would expect 09 to be the day. It should have been 2009 if meant to be the year (which your function does support).

Date := "January 09 9:05:27"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd h:mm:ss

;outputs "April 12 9:05:00"
MsgBox, % FormattedTime

As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Date := "January 09 9:05:27"

Date := "January 2nd, 2009"

My regex was written only for the British date notation which is day-month-year.

Date := "January 08, 2009 9:05:27"

Seems like the code "(?:(Z)|(\+|\-)?" . "(\d{1,2})\6(\d{1,2})" was supposed to handle seconds and time zone offsets as per ISO 8601 but I didn't bother completing this.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008
Given that, would you be offended if I released my own version of DateParse?

(Edit) I removed the exta, as I realized, I presumed things that I shouldn't have.
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006

Given that, would you be offended if I released my own version of DateParse?

(Edit) I removed the exta, as I realized, I presumed things that I shouldn't have.



hopefully we all learn from & teach each other. Multiple approaches of same idea in different ways seems beneficial all around.
Joyce Jamce

animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008
Thanks, I always worry that it might come off as "territorial" - like I was trying to outwrite someone else's existing code.
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.

mikek
  • Members
  • 161 posts
  • Last active: Nov 09 2015 05:02 PM
  • Joined: 21 Nov 2008

I needed to make the following change to make American Date Format mm/dd/yyyy hh:mm.... work:

DateParse(str) {
	static e2 = "i)(?:(\d{1, ; [...]
[color=violet]	str := RegExReplace(str, "(\d{1,2})(\D+)(\d{1,2})(.*)", "$3$2$1$4")[/color]

This works great! Thank you, Titan, and empyrean5 for the modification.

- Mike

pajenn
  • Members
  • 391 posts
  • Last active: Feb 06 2015 07:57 AM
  • Joined: 07 Feb 2009
I'm in need of the function addressed by this script (US version), however, it doesn't work when I add in seconds. For example,

MsgBox % DateParse("3/15/2009 11:22 AM")
;MsgBox message = 200903151122 (correct)

MsgBox % DateParse("3/15/2009 11:22:24 AM")
;MsgBox message = 200903152322 (incorrect)

Is there something I can do to enable the seconds?

Hardware: fast laptop with SSD
Software: Win 7 Home Premium 64-bit, android for phone and tablet


alpha
  • Guests
  • Last active:
  • Joined: --

I'm in need of the function addressed by this script (US version), however, it doesn't work when I add in seconds. For example,

MsgBox % DateParse("3/15/2009 11:22 AM")
;MsgBox message = 200903151122 (correct)

MsgBox % DateParse("3/15/2009 11:22:24 AM")
;MsgBox message = 200903152322 (incorrect)

Is there something I can do to enable the seconds?

Yes, dig in and change the regex. That is what I did (it about drove me nuts getting it right). :)

Here is the code I used. I works with your example
MsgBox % DateParse("3/15/2009 11:22:24 AM") ; result = 20091503112224

DateParse(str) {
	static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
	str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
		d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8, t4 := t1 >11 ? "pm" : "am"
	Else If !RegExMatch(str, "x)^\W*   (\d{1,2}?)   (\d{2})   (\d{2}+)? (?:\s*([ap]m))?  \W*  $", t)
		RegExMatch(str, "ix)(\d{1,2})  \s*  :  \s*  (\d{1,2})   (?:(?:\s*:\s*) (\d{1,2}))?   (?:\s*([ap]m))?", t)
			, RegExMatch(str, e2, d)
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	d := (d3 ? (StrLen(d3) = 2 ? 20 : "") . d3 : A_YYYY)
		. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
			? d2 + 0.0 : A_MM). ((d1 += 0.0) ? d1 : A_DD) . t1

	SetFormat, Float, %f%
	Return, d
}