Just playing here I found this which begins to do what I am looking for, but I need it to be more precise.
For example
date "Monday, 7 January 2013 16:05:04"
I would like it to look like this.
20130107160504
then later use it as a file name?
Just playing here I found this which begins to do what I am looking for, but I need it to be more precise.
For example
date "Monday, 7 January 2013 16:05:04"
I would like it to look like this.
20130107160504
then later use it as a file name?
Posted on Jan 7, 2013 8:06 AM
Jan 7, 2013 8:07 AM
opps I mean I want it to look like this
2013-01-07-1605-04
Jan 7, 2013 8:42 AM
I am not an Applescript god but here is something that should work to convert the current date to the requested format and place it on the clipboard. I assumed you also wanted a dash between the hour and minute. If not, you can remove it.
set yearstr to year of (current date)
set monthnum to month of (current date) as integer
if monthnum < 10 then
set monthstr to "0" & (monthnum as string)
else
set monthstr to (monthnum as string)
end if
set daynum to (day of (current date)) as integer
if daynum < 10 then
set daystr to "0" & (daynum as string)
else
set daystr to (daynum as string)
end if
set t to time of (current date)
set hournum to (round (t / 3600) rounding down)
if hournum < 10 then
set hourstr to "0" & (hournum as string)
else
set hourstr to (hournum as string)
end if
set t to t - hournum * 3600
set minnum to (round (t / 60) rounding down)
if minnum < 10 then
set minstr to "0" & (minnum as string)
else
set minstr to (minnum as string)
end if
set t to t - minnum * 60
if t < 10 then
set secstr to "0" & (t as string)
else
set secstr to (t as string)
end if
set the clipboard to(yearstr & "-" & monthstr & "-" & daystr & "-" & hourstr & "-" & minstr & "-" & secstr) asstring
Jan 7, 2013 9:37 AM
That's pretty good, Bad, but I'd make a couple of changes.
First off, I'd get the date once at the beginning of the script, rather than at muliple points through it:
set d to (get current date)
It's a small point, but there is some overhead in calling the 'current date' command, so calling it once and saving the result is more efficient. There's also a (very small) possibility that the date could change in the middle of the script execution 00004000 - for example if you run your script a few milliseonds before midnight there's a chance you get the date of one day and the time of the next day. Getting the date once eliminates this.
There's also a more efficient way of extracting the time components - AppleScript knows about things like 'hours', 'minutes' and 'seconds', so you can do things like:
set t to time of d
set h to t div hours
set m to t mod hours div minutes
set s to t mod minutes
You still need to do the string-mangling to prepend '0' to single-digit numbers, but this is more efficient (and readable).
Jan 7, 2013 9:49 AM
Yes, getting the current date once at the beginning is a very good idea. Not so much for the overhead but for the off chance the time changes to the next minute (which by itself would be enough to give wrong results) while the script is running.
Thanks for the hints. There is always more to be learned.I never needed to do anything with "current time" before so I was learning as I wrote it. It was a first cut.
I hope the OP makes the suggested changes.
Jan 7, 2013 10:12 AM
Good point, thats all working well
Many Thanks all
Originally posted at discussions.apple.com/thread/4682212