Re: Getting time from Current Date
Re: Getting time from Current Date
- Subject: Re: Getting time from Current Date
- From: David Reynolds <email@hidden>
- Date: Thu, 22 Feb 2001 17:08:18 -0800
on 2001.02.23 12:00 AM, Edmond Cho at email@hidden said:
>
The scripting additions guide doesn't really go very deeply into Current
>
Date. When I try to access the (time of (current date)), it returns an
>
integer (seconds). Is there a way to access it in the format in which it
>
appears as a whole (Day Month Date Year 00:00:00 AM)?
>
>
00:00:00 AM instead of 00001?
>
>
I want to avoid using any scripting additions aside from the standard
>
supplied ones.
Here's a script I wrote to do exactly that. It's more than fully featured,
but I didn't want to rewrite any of it ever again!
Use Richard 23's most excellent ConvertScript script to get this back to
standard AppleScript:
<
http://homepage.mac.com/richard23/applescript03.html#ConvertScript>
-- ---------------------------------------------------------
-- Preprocessed by Convert Script 1.0d3
-- ---------------------------------------------------------
--my_date : "" -- this gets the full date time stamp from the OS, otherwise
heach function would be working with a moving target
-- my_year : "" -- four-digit gregorian calendar year
-- my_2digit_year: "" -- old y2K two-digit year
-- my_month : "" -- month as full string "Crudtember"
-- my_abbreviated_month : "" -- month as abbrev. "Crudt."
-- my_MMM : "" -- month as old school capped three char abbrev (FEB)
-- my_numeric_month : "" -- month as numeric representation, with leading
zero "02"
-- my_day : "" -- day number, ""
-- my_padded_day : "" --day number with leading 0 "05"
-- my_ordinal : "" -- th, st, nd, rd, whatever
-- my_weekday : "" -- Friday!
-- my_abbreviated_weekday:"" -- Fri.!
-- my_mil_time : "" --1823 is 6:23 PM, got it?
-- my_24hr : "" -- international time
-- my_hour : "" -- "normal" 12 hour clock
-- my_min : ""
-- my_sec : ""
-- my_meridian : "" -- morning or night? (AM/PM)
whatTimeIsIt()
on whatTimeIsIt()
--DATE FUNCTIONS
set my_date to current date
set my_year to year of my_date
set my_2digit_year to (characters 3 thru 4 of (my_year as string)) as
string
set my_month to month of my_date
set my_day to day of my_date
set my_weekday to weekday of my_date
--since the month is returned as a full string ("Crudtober") here's a
method to get the numeric representation
repeat with my_numeric_month from 1 to 12
if my_month = item my_numeric_month of ==>
{January, February, March, April, May, June, July, August,
September, October, November, December} then exit repeat
end repeat
--and here's where we get the abbrev. mon. versions
set my_abbreviated_month to item my_numeric_month of {"Jan.", "Feb.",
"Mar.", "Apr.", "May", "June", "July", " Aug.", "Sept.", "Oct.", "Nov.",
"Dec."}
if my_numeric_month < 10 then
set my_numeric_month to ("0" & my_numeric_month) as string
end if
--and for those who like traditional military style month abbreviations
(FEB)...
set my_temp to (characters 1 thru 3 of my_abbreviated_month) as string
set my_MMM to character 1 of my_temp
repeat with x from 2 to 3
set my_MMM to my_MMM & (ASCII character ((ASCII number (character x
of my_temp)) - 32)) as string
end repeat
--need a leading "0" in the date?
if my_day < 10 then
set my_padded_day to ("0" & my_day) as string
else
set my_padded_day to my_day
end if
--abbreviated weekdays
repeat with x from 1 to 7
if my_weekday = item x of {Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday} then
exit repeat
end if
end repeat
set my_abbreviated_weekday to item x of {"Sun.", "Mon.", "Tue.", "Wed.",
"Thu.", "Fri.", "Sat."}
-- the date may be crudtember n, but should it read the n-teenth or
n-third?
--BTW: this routine will give the correct ordinal for *ANY* number, from
1 (1st) to n (n-th)
--It also works on numbers as strings, but it won't strip leading
zeros... the 04th would look stupid, but that's on you.
set my_ordinal to "th"
if ((count of characters of (my_day as string)) > 1) and (character
((count of characters of (my_day as string)) - 1) of (my_day as string) =
"1") then
else
set my_num to {"st", "nd", "rd"}
repeat with i from 1 to 3
if last character of (my_day as string) = i as string then
set my_ordinal to item i of my_num
end if
end repeat
end if
set my_full_ordinal to (my_day & my_ordinal) as string
-- date formats
set full_date to (my_weekday & "," & space & my_month & space &
my_full_ordinal & "," & space & my_year) as string
set mil_date to (my_padded_day & my_MMM & my_2digit_year) as string
set dev_date to (my_2digit_year & "." & my_numeric_month & "." &
my_padded_day) as string
set international_date to (my_day & "/" & my_month & "/" &
my_2digit_year) as string
set short_date to (my_numeric_month & "/" & my_padded_day & "/" &
my_2digit_year) as string
--more optional formats
set my_slashdate to (my_numeric_month & "/" & my_padded_day & "/" &
my_2digit_year) as string
set my_dashdate to (my_numeric_month & "-" & my_padded_day & "-" &
my_2digit_year) as string
--TIME FUNCTIONS
set my_time to time of my_date --this time function returns a value that
is the number of seconds from midnight - bloody useful, no?
--here's where we figure how many hours these seconds make up.
--for standard 12hr clock time
set my_24hr to my_time div 3600
set my_hour to my_24hr
if my_24hr > 12 then
set my_hour to my_24hr - 12
else
if my_hour = 0 then
set my_hour to 12
end if
end if
--for the 12hr clock we need AM/PM
if my_time < 43200 then
set my_meridian to "AM"
else
set my_meridian to "PM"
end if
--after figuring hours, we work with the remainder (mod) to get minutes
set my_min to (my_time mod 3600) div 60
timecheck(my_min)
set my_min to (result as text)
--and we use the mod of the my_min calculation to get seconds
set my_sec to (my_time mod 3600) mod 60
timecheck(my_sec)
set my_sec to (result as text)
--for international time
set my_Intl to (my_24hr & ":" & my_min & ":" & my_sec) as string
--for military time
set my_mil_time to (my_24hr & my_min) as string
--for US time
set US_time to (my_hour & ":" & my_min & ":" & my_sec & space &
my_meridian) as string
--this is why I wrote this @#$%^ library in the first place...
set post_date to (my_abbreviated_month & "," & space & my_full_ordinal)
as string
set post_time to (my_hour & ":" & my_min & space & my_meridian) as
string
set page_date to (my_month & "," & space & my_year) as string
set RECdatebits to {year:my_year, shortyear:my_2digit_year,
month:my_month, shortmonth:my_abbreviated_month, capshortmonth:my_MMM,
monthnumber:my_numeric_month, day:my_day, zeroday:my_padded_day,
ordinalday:my_full_ordinal, weekday:my_weekday,
shortweekday:my_abbreviated_weekday}
set RECtimebits to {time:my_time, twentyfour:my_24hr, hour:my_hour,
minutes:my_min, seconds:my_sec, meridian:my_meridian}
set RECdates to {longdate:full_date, shortdate:short_date,
military:mil_date, developer:dev_date, international:international_date,
post:post_date, page:page_date}
set RECtimes to {international:my_Intl, military:my_mil_time,
US:US_time, post:post_time}
return {RECdatebits, RECtimebits, RECdates, RECtimes}
end whatTimeIsIt
on timecheck(TIME_VAL)
if (count of characters of (TIME_VAL as text)) = 1 then
set TIME_VAL to "0" & TIME_VAL
end if
return TIME_VALend timecheck
-- ---------------------------------------------------------