String to date

Hi,

we need to convert a string using “pure” AS into date:

Example:

YYYYMMDDHHMMSS like 20090801204150 must be converted as:
“Saturday, 1 August 2009 20:40:59”

The script should works on both Tiger/Leopard.

If is not possible using AS maybe with Cocoa Call method?

Hagimeno

One way to go: (whether AM/PM or a 24-hour clock results depends on your Time Preferences)

set tTimeStamp to "20090806204150"
set MO to {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}

tell tTimeStamp
	set mon to item (text 5 thru 6 as integer) of MO
	set MacDate to my date (text 7 thru 8 & space & mon & space & text 1 thru 4)
	set tSecs to 3600 * text 9 thru 10 + 60 * text 11 thru 12 + text 13 thru 14
end tell
set time of MacDate to tSecs --> date "Thursday, August 6, 2009 8:41:50 PM"

Adam, when I run your script I get 74510 as a result. I don’t get a date at all.

So here’s my take on the problem. If you don’t really care about the 24 hour time format then this will suffice…

set timeStamp to "20090806204150"

-- convert the timeStamp into a date
tell timeStamp
	set y to text 1 thru 4
	set m to text 5 thru 6
	set d to text 7 thru 8
	set h to text 9 thru 10
	set min to text 11 thru 12
	set s to text 13 thru 14
end tell
set theDate to (date (m & "/" & d & "/" & y & space & h & ":" & min & ":" & s)) as string
--> "Thursday, August 6, 2009 8:41:50 PM"

If you always want 24 hour time format then you need a little more manipulation…

set timeStamp to "20090806204150"

-- convert the timeStamp into a date
tell timeStamp
	set y to text 1 thru 4
	set m to text 5 thru 6
	set d to text 7 thru 8
	set h to text 9 thru 10
	set min to text 11 thru 12
	set s to text 13 thru 14
end tell
set theDate to date (m & "/" & d & "/" & y & space & h & ":" & min & ":" & s) --> date "Thursday, August 6, 2009 8:41:50 PM"

-- fix the AM/PM stuff changing the hour into 24 hour format
set timeString to (time string of theDate) --> "8:41:50 PM"
if text -2 thru -1 of timeString is "PM" then
	set text item delimiters to ":"
	set timeList to text items of timeString
	set twentyfourHourFormat to (((item 1 of timeList) as number) + 12) as text
	set item 1 of timeList to twentyfourHourFormat
	set timeString to timeList as string
	set text item delimiters to ""
end if
set timeString to text 1 thru -4 of timeString

-- compile the final date string in the desired format
set finalDateString to date string of theDate & space & timeString
--> "Thursday, August 6, 2009 20:41:50"

Hi,

I need that routine must be independent from a System Prefs date set.
I mean that routine must return 8 Aug 2009 even if the routine run on USA Mac or European Mac that handle date in different format (USA: MMDDYYYY and Euro as DD MM YYYY).
So giving input YYYYMMDD I need to return the correct date for every Mac user in the world.
Maybe using do shell script and date?

S.H

Hi,
Correction:
I need that routine must be independent from a System Prefs date set.
I mean that routine must return 6 Aug 2009 (20090806) even if the routine run on USA Mac or European Mac that handle date in different format (USA: MMDDYYYY and Euro as DD MM YYYY).
So giving input YYYYMMDD I need to return the correct date for every Mac user in the world.
Maybe using do shell script and date?

S.H

Well I think between what Adam showed you and what I showed you, you have enough information to try it yourself to make it into any format you want. Give it a try and if you still have trouble post your code and ask for more help. The search function of the forums is a terrific way to find stuff.

I understand the scripts posted but both don’t solve the problem because on both scripts date are specified in specific format.

S.H

That’s the result of setting the time in Adam’s last line. MacDate itself then has the correct value. :slight_smile:

This should work on any user’s machine from Panther on, returning a date text in the user’s preferred format:

set tTimeStamp to "20090806204150"

set n to tTimeStamp as number
-- Adjust the source code for the following date to the machine compiling the script.
-- Once compiled, it'll work on any machine.
set ASDate to date "Wednesday 1 January 1000 00:00:00"
tell ASDate to set {its year, its month, its day, its time} to {n div 1.0E+10, n mod 1.0E+10 div 100000000, n mod 100000000 div 1000000, n mod 1000000 div 10000 * hours + n mod 10000 div 100 * minutes + n mod 100}

return ASDate as text

Hi Nigel,

Thanks! This is “The” solution :wink:

Steve