Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Convert current date to YYYYMMDDHHMMSS for use as filename?

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?

iMac, Mac OS X (10.6.8)

Posted on Jan 7, 2013 8:06 AM

Reply
5 replies

Jan 7, 2013 8:42 AM in response to MattJayC

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 in response to Badunit

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 - 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 in response to Camelot

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.

Convert current date to YYYYMMDDHHMMSS for use as filename?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.