Monday, May 25, 2020

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

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

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: 4344

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)

Online

 

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

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

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: 4344

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

Online

 

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

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
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: 570

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: 4344

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

Online

 

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

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

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

Online

 

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

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

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

Online

 

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

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
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: 6362

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

Online

 

#12 Yesterday 08:41:29 am

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

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 Yesterday 11:44:10 am

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

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

Online

 

#14 Today 07:33:38 am

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

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 Today 07:39:12 am

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

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

Online

 

#16 Today 03:03:33 pm

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

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

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)