I often use this handler to date_time stamp files.
#=====
on horoDateur()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd hh:mm:ss"
return (theFormatter's stringFromDate:(current date)) as text
end horoDateur
#=====
I am asked to edit it to replace the hh:mm:ss component by the time value in seconds.
Is it a way to edit the string_format to achieve that or must have to use:
#=====
on horoDateur()
set theDate to current date
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd "
return ((theFormatter's stringFromDate:theDate) as text) & time of (theDate)
end horoDateur
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 8 aout 2019 16:38:36
Oops, it seems that there is again something wrong with my tags
use framework "Foundation"
use scripting additions
set theDate to current application's NSDate's new()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd SSSS"
return (theFormatter's stringFromDate:theDate)
I don’t think it does. What SSSS returns is the fraction of a second, and you see that only with an NSDate because AppleScript dates don’t have fractions of a second.
The ICU don’t have a symbol for seconds of a day — you should log a feature request with them . However, they do have one for milliseconds, A. So:
on horoDateur()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd AAAA" -- or AAAAAAAA for leading 0s
return text 1 thru -4 of ((theFormatter's stringFromDate:(current date)) as text)
end horoDateur
I’m a bit surprised that works — interesting. The typical method is:
set theDate to current application's NSDate's |date|()
Anyway, with a combination of an NSDate and using A, Yvan can stop trimming the last three digits and get unique stamps even if calls are made less than a second apart.
NSDate has an init method that returns the current date.
I naively thought that new() could be a shortcut for alloc()'s init()…
Do you think it’s not safe to use it?
No, it’s fine — it was just a surprise to me because I’ve never seen it done like that before. But now I check I see it’s documented, so it’s all good. And no pipes
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
#=====
# This one does what I asked for
on horoDateur() # without milliseconds
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd A"
return text 1 thru -4 of ((theFormatter's stringFromDate:(current date)) as text)
end horoDateur
#=====
on horoDateur2() # with milliseconds
set theDate to current application's NSDate's |date|() # the 'complete' current date
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd A"
return (theFormatter's stringFromDate:theDate) as text
end horoDateur2
#=====
on horoDateur3()
set theDate to current application's NSDate's new() # the current date
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd SSSS"
return (theFormatter's stringFromDate:theDate) as text
end horoDateur3
#=====
on horoDateur4()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd SSSS"
return (theFormatter's stringFromDate:(current date)) as text
end horoDateur4
#=====
# This one does what I asked for
on horoDateur5(theDate) # without milliseconds
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd A"
return text 1 thru -4 of ((theFormatter's stringFromDate:theDate) as text)
end horoDateur5
#=====
log my horoDateur() --> (*2019-08-09 54551*)
log my horoDateur2() --> (*2019-08-09 54551161*)
log my horoDateur3() --> (*2019-08-09 1610*)
log my horoDateur4() --> (*2019-08-09 0000*)
log my horoDateur5((current date) - 10 * days) --> (*2019-07-30 54551*)
And this time I used the tags correctly
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 9 aout 2019 15:18:04
The only real difference I can see are the addition of b and B. They’re supported in 10.14 here, and I suspect they’ve been around for a while.
One other thing, though: the explanation of A is clearer, in that it adds: “The field length specifies the minimum number of digits, with zero-padding as necessary.” So if you want date strings for file names that sort time-wise, you’d probably want to use AAAAAAAA rather than A.
At last I understood the use of A.
If I put a single A, the instruction return the count of milliseconds. which may use 8 digits.
So, if I want the time component displayed with a fixed length of 8 digits, I must use a format like “yyyy-MM-dd AAAAAAAA”
Of course, I must use the same string if I want to display the seconds of an AppleScript date with 5 digits.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 10 aout 2019 17:01:47
They’re ignored on my El Capitan machine. On the Mojave one, their effects are identical to each other’s and only differ from a’s in producing “noon” instead of “pm” at midday. None of that “midnight” or “at night” stuff.
Right. What’s more, you need to use at leastAAAA if you intend to trim off the milliseconds, otherwise you will get a mangled date during the first second of the day. I’m going to amend my earlier post accordingly.
Thank you Nigel and Shane.
It’s fine to learn new details from time to time.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 10:20:15