Sunday, July 12, 2020

#1 2020-05-22 08:04:19 am

olli
Member
Registered: 2008-11-01
Posts: 38

Convert a string into a date class

I have a string with date time information and I need to convert this into a date class.

The string is in the following format:

Applescript:

"2020-05-07T06:05:34Z"

How can I convert this into a date class?


Filed under: conversion, date

Offline

 

#2 2020-05-22 08:30:02 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

Here is a script borrowed from Shane Stanley's Everyday AppleScriptObjC.
I just added the Z parameter and changed the string between date part and time part and choose to require at least macOS 10.11

Applescript:

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
on dateFromString:aString usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setDateFormat:formatString
   set theDate to theFormatter's dateFromString:aString
   return theDate as date
end dateFromString:usingFormat:

its dateFromString:"2020-05-07T06:05:34Z" usingFormat:"yyyy-MM-dd'T'HH:mm:ssZ"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 22 mai 2020 16:29:56

Last edited by Yvan Koenig (2020-05-22 08:34:46 am)

Offline

 

#3 2020-05-22 08:47:28 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 576

Re: Convert a string into a date class

Caution: that text format implies UTC time; the returned date object uses local time. With the given string I get

Applescript:

date "donderdag 7 mei 2020 08:05:34"

which is correct for my time zone (CEST = Europe/Amsterdam, summer time).

Offline

 

#4 2020-05-22 10:26:10 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

alastor933 wrote:

Caution: that text format implies UTC time; the returned date object uses local time. With the given string I get

Applescript:

date "donderdag 7 mei 2020 08:05:34"

which is correct for my time zone (CEST = Europe/Amsterdam, summer time).



What need for this "Caution". The date string passed is using a perfect syntax for a local date.

Here it returns : date "jeudi 7 mai 2020 à 08:05:34"

its dateFromString:"07-05-2020T06:05:34Z" usingFormat:"dd-MM-yyyy'T'HH:mm:ssZ"

would return the same result.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 22 mai 2020 18:25:53

Offline

 

#5 2020-05-22 10:38:00 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11688
Website

Re: Convert a string into a date class

There is a special date formatter for ISO8601 dates

Applescript:

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set dateString to "2020-05-07T06:05:34Z"
set formatter to current application's NSISO8601DateFormatter's new()
set theDate to (formatter's dateFromString:dateString) as date


regards

Stefan

Offline

 

#6 2020-05-22 11:17:37 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 576

Re: Convert a string into a date class

Yvan Koenig wrote:

What need for this "Caution". The date string passed is using a perfect syntax for a local date.

The time going in is "06:05:34". The time coming out is "08:05:34", in my location.
Olli may not be wanting this.
And, not being aware of this, it may bite you:

Applescript:

use framework "Foundation"
on dateFromString:aString usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setDateFormat:formatString
   set theDate to theFormatter's dateFromString:aString
   return theDate as date
end dateFromString:usingFormat:

its dateFromString:"2020-05-07T22:05:34Z" usingFormat:"yyyy-MM-dd'T'HH:mm:ssZ"
--> date "vrijdag 8 mei 2020 00:05:34"
-- which is the NEXT day!

Offline

 

#7 2020-05-22 11:29:39 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

alastor933 wrote:
Yvan Koenig wrote:

What need for this "Caution". The date string passed is using a perfect syntax for a local date.

The time going in is "06:05:34". The time coming out is "08:05:34", in my location.
Olli may not be wanting this.
And, not being aware of this, it may bite you:

Applescript:

use framework "Foundation"
on dateFromString:aString usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setDateFormat:formatString
   set theDate to theFormatter's dateFromString:aString
   return theDate as date
end dateFromString:usingFormat:

its dateFromString:"2020-05-07T22:05:34Z" usingFormat:"yyyy-MM-dd'T'HH:mm:ssZ"
--> date "vrijdag 8 mei 2020 00:05:34"
-- which is the NEXT day!



And this behavior is the correct one.

If he doesn't want the described behavior he would use the format : "2020-05-07T22:05:34".
I guess that the final Z is not a typo !

Applescript:

use framework "Foundation"
on dateFromString:aString usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setDateFormat:formatString
   set theDate to theFormatter's dateFromString:aString
   return theDate as date
end dateFromString:usingFormat:

its dateFromString:"2020-05-07T22:05:34" usingFormat:"yyyy-MM-dd'T'HH:mm:ss"

As stefanK wrote, the used format (with the ending "Z") is one of those with an official name.
As far as I know, the one without the "Z"  hasn't a name.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 22 mai 2020 19:29:27

Offline

 

#8 2020-05-22 06:06:24 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6403

Re: Convert a string into a date class

StefanK wrote:

There is a special date formatter for ISO8601 dates



But it's worth keeping in mind that it requires macOS 10.12, and its enums require 10.13.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2020-05-22 06:07:51 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6403

Re: Convert a string into a date class

alastor933 wrote:

The time going in is "06:05:34". The time coming out is "08:05:34", in my location.
Olli may not be wanting this.



The "Z" means GMT/UTC time zone, so this difference is required.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2020-05-22 10:55:07 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11688
Website

Re: Convert a string into a date class

Shane Stanley wrote:



But it's worth keeping in mind that it requires macOS 10.12, and its enums require 10.13.



Of course, but Sierra users are very rare.

And what enums are you talking about? Only the fractional seconds format option requires 10.13


regards

Stefan

Offline

 

#11 2020-05-22 11:02:10 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6403

Re: Convert a string into a date class

StefanK wrote:

what enums are you talking about?



The NSISO8601DateFormatOptions enums (not needed in this example). Although they are part of 10.12, they were left out of its Foundation.bridgesupport file.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#12 2020-05-23 08:41:29 am

olli
Member
Registered: 2008-11-01
Posts: 38

Re: Convert a string into a date class

Firstly thanks for the solution and all the comments! Greatly appreciated!

alastor933 wrote:
Yvan Koenig wrote:

What need for this "Caution". The date string passed is using a perfect syntax for a local date.

The time going in is "06:05:34". The time coming out is "08:05:34", in my location.
Olli may not be wanting this.

Well, I do want this. The time is indeed GMT, while I would like to see my local time.

So I can use the code as is. :-)

ps: The code has lots of stuff I have never seen before. Particularly the colon notation like

Applescript:

":aString"

is totally new to me!

Offline

 

#13 2020-05-23 11:44:10 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

olli wrote:

Firstly thanks for the solution and all the comments! Greatly appreciated!

alastor933 wrote:
Yvan Koenig wrote:

What need for this "Caution". The date string passed is using a perfect syntax for a local date.

The time going in is "06:05:34". The time coming out is "08:05:34", in my location.
Olli may not be wanting this.

Well, I do want this. The time is indeed GMT, while I would like to see my local time.

So I can use the code as is. :-)

ps: The code has lots of stuff I have never seen before. Particularly the colon notation like

Applescript:

":aString"

is totally new to me!




Don't worry, it was new for me too when I read  Shane Stanley's "Everyday AppleScriptObjC" for the first time.

The relevant part is :

There is one last thing to do. In both the handler and where it is being called, insert an underscore after the name so that cleanUpText(someText) becomes cleanUpText_(someText), and compile the script. You will see that the handler declaration changes to read cleanUpText:someText. The two forms are equivalent, and compile to identical underlying code. The interleaved version was only introduced in Mavericks — before that, you would also have used stringByReplacingOccurrencesOfString_withString_options_range_(...), for example, with the arguments in the parentheses and separated by commas.
The interleaved syntax can be used in AppleScript generally — it is not confined to AppleScriptObjC. You may see the change happen elsewhere if you use underscores in your handler names.

Maybe Shane would give extraneous explanations. wink

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 23 mai 2020 19:44:04

Offline

 

#14 2020-05-24 07:33:38 am

olli
Member
Registered: 2008-11-01
Posts: 38

Re: Convert a string into a date class

Yvan Koenig wrote:


The interleaved syntax can be used in AppleScript generally — it is not confined to AppleScriptObjC.



But what is the basic purpose of the colon? Indication of parameters to functions?

Offline

 

#15 2020-05-24 07:39:12 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6403

Re: Convert a string into a date class

olli wrote:

Indication of parameters to functions?



Yes. It's described in the AppleScript Language Guide as "Handlers with Interleaved Parameters".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#16 2020-05-24 03:03:33 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

Thank you Shane, I forgot this chapter.

On my side I use such syntax when I use ASObjC and remain to the old fashioned one when I code the old way.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mai 2020 23:03:15

Offline

 

#17 2020-05-24 08:31:45 pm

olli
Member
Registered: 2008-11-01
Posts: 38

Re: Convert a string into a date class

Shane Stanley wrote:

It's described in the AppleScript Language Guide as "Handlers with Interleaved Parameters".



Thanks heaps, Shane. Found it in the AppleScript Language Guide .

Offline

 

#18 2020-05-26 09:40:16 am

robertfern
Member
Registered: 2011-11-29
Posts: 64

Re: Convert a string into a date class

I was able to di it with pure Applscript using the built in date function

Applescript:


set sDate to "2020-05-07T06:05:34Z"

set cdate to date ((text 6 thru 7 of sDate) & "/" & (text 9 thru 10 of sDate) & "/" & (text 1 thru 4 of sDate) & " at " & (text 12 thru 19 of sDate) & " " & (item (((text 12 thru 13 of sDate) as integer) div 12 mod 2 + 1) of {"AM", "PM"}))

you have to calculate the AM/PM part or the time portion of the resulting date will be default at 12:00:00 AM

-- add time to get to adjust for local time

Applescript:

set cdate to cdate + (time to GMT)

Last edited by robertfern (2020-05-26 10:24:47 am)

Offline

 

#19 2020-05-26 10:10:58 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4553

Re: Convert a string into a date class

robertfern wrote:

I was able to di it with pure Applscript using the built in date function

Applescript:


set sDate to "2020-05-07T06:05:34Z"

set cdate to date ((text 6 thru 7 of sDate) & "/" & (text 9 thru 10 of sDate) & "/" & (text 1 thru 4 of sDate) & " at " & (text 12 thru 19 of sDate) & " " & (item (((text 12 thru 13 of sDate) as integer) div 12 mod 2 + 1) of {"AM", "PM"}))

you have to calculate the AM/PM part or the time portion of the resulting date will be default at 12:00:00 AM



Have you ever ran my proposal ?
It returns the correct date but yours fails.

When it's executed in France where the offset from Greenwich is 2 hours in the given period,
your script return dimanche 5 juillet 2020 à 00:00:00 (and it's unable to take cate of the summer time component)
when the true result is: jeudi 7 mai 2020 à 06:05:34.

The posted date uses the format "yyyy-MM-dd'T'HH:mm:ssZ"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 mai 2020 18:09:13

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)