current date - 24 hr clock [isot]

Hello all,

how can you display the current time in as 24-hr clock using current date?

tia

for example like this:

tell (current date) to set t to (its hours as string) & ":" & (its minutes as string) & ":" & its seconds as string
display dialog t

 time string of (current date)

gives me “21:20:00”
(may depend on your settings in PrefPane date/time)

thanks this works great.

Eelco Houwink, it’s because of your preferences

Hi Eelco,

it depends on date format settings in International PrefPane.
Most europeans use 24 hour format anyway :wink:

or…

set the_hour to do shell script "date +%H:%M:%S"
display dialog the_hour

incidentally. I changed my date/time to be displayed in 12h time. while testing all three scripts.
All returned 24h

EDIT Just seen Stefans post, changed the International pref.
The ’ time string ’ script reflects whats in the International pref.

This should also work (the coercion is implicit):

tell (current date) to set theTime to "" & its hours & ":" & its minutes & ":" & its seconds

Side note: I’ve never noticed that (the grammatically correct) its can be used; That’s nice to know. :slight_smile:

In this case ‘its’ must be used, because hours, minutes and seconds without a reference are the AppleScript constants

Hi, all.

This works with any version of Mac OS and gives leading zeros where required:

tell time of (current date) to tell (1000000 + it div hours * 10000 + it mod hours div minutes * 100 + it mod minutes) as string to set theTime to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7

Yes; I’m was thinking of its vs. it's.

I need to add this kind of stuff to my library.

Then there’s:

set theTime to text -8 thru -1 of ((current date) as «class isot» as string)

Where did you pick that up, kai? I’m guess that means “ISO time” (or something similar).

Nigel and I first came across it on another list, Bruce. It doesn’t offer the same legacy as the date math approach, and certainly won’t work pre-OS X, but can be quite handy in some situations. The result returned is one of the combined representations from the ISO 8601 specification. :slight_smile:

Thanks. :slight_smile:

I love how when the post progresses, the code used to satisfy the poster gets shorter and shorter. I guess kai wins! :D:P

Thanks for pointing out that one, kai. I’d forgotten about it in the heat of the moment. As you say, it doesn’t work in OS 9 or earlier, but it’s certainly OK at least as far as back as Jaguar.

I was thinking the other day that the reverse process might be used to overcome the problem of representing preset dates in AppleScript fora. I sometimes like to calculate a date based on, say, a known Sunday in the past:

set baseDate to date "Sunday 5 January 1000 00:00:00"

This is OK in a compiled script, but when distributing it as text, as here, it means that many people will need to edit the date specifier into their own language before it will compile. (Stefan’s pointed this out in the past.) One way round this is to get the ‘current date’ and set all the result’s properties to the required values. But the following should also work on any OS X system:

set baseDate to "1000-01-05" as «class isot» as date

Or even:

set baseDate to «data isot313030302D30312D3035» as date

The technique undeniably removes any concern over such issues, Mr. G. Very nice.

In addition, since starting with the raw data halves the number of coercions involved, it’s also (nearly) twice as fast as the text-to-data-to-date method. :slight_smile: