Re: Date Routines
Re: Date Routines
- Subject: Re: Date Routines
- From: Paul Skinner <email@hidden>
- Date: Thu, 7 Aug 2003 14:27:41 -0400
On Thursday, August 7, 2003, at 12:17 PM, Steve Zaspel wrote:
Does anyone know of any good date calculation routines that are
available?
It would sure save me alot of time on a project I'm working on. Thanks
for
your help.
This code's a bit old, but reliable.
DateAndTime(null)
-->{timestamp:"142124", dateStamp:"030807",
datetimeStamp:"030807142124", mdyy:"8_7_03", mmddyy:"08_07_03",
mdyyyy:"8_7_2003", hms:"2:21:24", h24ms:"14:21:24", hhmmss:"02:21:24",
hh24mmss:"14:21:24", yyyy:"2003", yy:"03", MonthIndex:"8",
mmonthIndex:"08", monthText:"August", abrMonthText:"Aug", d:"7",
dd:"07", dayText:"Thursday", abrDayText:"Thu", H:"2", hh:"02",
h24:"14", hh24:"14", m:"21", mm:"21", s:"24", ss:"24",
meridianValue:"PM"}
DateAndTime({givenDate:date "Thursday, July 4, 1776 12:00:00 AM"})
-->{timestamp:"000000", dateStamp:"760704",
datetimeStamp:"760704000000", mdyy:"7_4_76", mmddyy:"07_04_76",
mdyyyy:"7_4_1776", hms:"12:0:0", h24ms:"0:0:0", hhmmss:"12:00:00",
hh24mmss:"00:00:00", yyyy:"1776", yy:"76", MonthIndex:"7",
mmonthIndex:"07", monthText:"July", abrMonthText:"Jul", d:"4", dd:"04",
dayText:"Thursday", abrDayText:"Thu", H:"12", hh:"12", h24:"0",
hh24:"00", m:"0", mm:"00", s:"0", ss:"00", meridianValue:"AM"}
hhmmss of DateAndTime({timeDelimiter:"*"})
-->"02*24*20"
mdyyyy of DateAndTime({})
-->"8_7_2003"
{yyyy, m, d} of DateAndTime({})
-->{"2003", "25", "7"}
on DateAndTime(parameters)
try --Main body try wraper. Handles errors the body code does not
handle.
-->>HANDLER DOCUMENTATION:<<--------------------------------------
set handlerName to "DateAndTime"
if parameters is missing value then --Return documentation for this
handler to the user.
set handlerExample to
"DateAndTime({dateDelimiter:\"_\",timeDelimiter:\":\",givenDate:date
\"Monday, August 6, 2001 2:18:18 PM\"})"
set handlerDescription to "DateAndTime theDescriptionOfThisHandler."
set handlerRequiredParameters to anything
set handlerOptionalParameters to
"{dateDelimiter:\"_\",timeDelimiter:\":\",givenDate:date SomedateValue"
set handlerVersionHistory to {"1.0"}
set handlerCreationDate to date "Tuesday, August 7, 2001 12:00:00 AM"
set handlerModificationDate to date "Monday, March 18, 2002 11:36:06
PM"
set handlerAuthors to {{author:"Paul Skinner",
email:"email@hidden"}}
set handlerRequirements to {osVersion:{"8.5", "8.5.1", "8.6", "9",
"9.1"}, asVersion:{"1.5.5", "1.6"}, osax:{"na"}}
set handlerOutput to "{timestamp:timestamp, dateStamp:dateStamp,
datetimeStamp:datetimeStamp , mdyy:mdyy, mmddyy:mmddyy, mdyyyy:mdyyyy
, hms:hms, h24ms:h24ms, hhmmss:hhmmss, hh24mmss:hh24mmss , yyyy:yyyy,
yy:yy, monthIndex:monthIndex, mmonthIndex:mmonthIndex,
monthText:monthText, abrMonthText:abrMonthText, d:d, dd:dd,
dayText:dayText, abrDayText:abrDayText , H:H, hh:hh, h24:h24,
hh24:hh24, m:m, mm:mm, s:s, ss:ss , meridianValue:meridianValue}."
return {handlerName:handlerName, handlerExample:handlerExample,
handlerDescription:handlerDescription,
handlerRequiredParameters:handlerRequiredParameters,
handlerOptionalParameters:handlerOptionalParameters,
handlerOutput:handlerOutput, handlerCreationDate:handlerCreationDate,
handlerModificationDate:handlerModificationDate,
handlerAuthors:handlerAuthors,
handlerVersionHistory:handlerVersionHistory,
handlerRequirements:handlerRequirements}
end if
-->>REQUIRED PARAMETER variable
assignments:<<--------------------------
--none
-->>OPTIONAL PARAMETER variable
assignments:<<--------------------------
--none
try
set currentDate to givenDate of parameters
--Make sure that the givenDate is in date format.
if class of currentDate is not date then
try
set currentDate to (date currentDate)
on error
return handlerInfo
end try
end if
on error
set currentDate to current date
end try
--
try
set dateDelimiter to dateDelimiter of parameters
on error
set dateDelimiter to "_"
end try
--
try
set timeDelimiter to timeDelimiter of parameters
on error
set timeDelimiter to ":"
end try
--
>>BODY:<<---------------------------------------------------------------
-------------
set previousTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set yyyy to the year of currentDate as text
set yy to characters -2 thru -1 of yyyy as text
set MonthIndex to (((offset of (the month of (currentDate)) in "jan
feb mar apr may jun jul aug sep oct nov dec ") + 4) div 4) as text
try
set mmonthIndex to characters 1 thru 2 of MonthIndex as text
on error
set mmonthIndex to "0" & MonthIndex as text
end try
set monthText to the month of currentDate as text
set abrMonthText to (characters 1 thru 3 of monthText) as text
set d to the day of currentDate as text
try
set dd to characters 1 thru 2 of d as text
on error
set dd to "0" & d as text
end try
set dayText to weekday of currentDate as text
set abrDayText to (characters 1 thru 3 of dayText) as text
set h24 to ((the time of currentDate) / hours) div 1 as text
set hh24 to h24 as text
if hh24 as integer is less than 9 then
set hh24 to "0" & hh24 as text
end if
if h24 as integer is greater than 12 then
set H to h24 - 12 as text
else
if h24 is "0" then
set H to 12 as text --compensate for 0 hour.
else
set H to h24
end if
end if
try
set hh to characters 1 thru 2 of H as text
on error
set hh to "0" & H as text
end try
set meridianValue to item (1 + (h24 div 12)) of {"AM", "PM"} as text
set m to (((the time of currentDate) / minutes) - (the h24 *
minutes)) div 1 as text
try
set mm to characters 1 thru 2 of m as text
on error
set mm to "0" & m as text
end try
set s to ((the time of currentDate) - (the h24 * hours) - (the m *
minutes)) div 1 as text
try
set ss to characters 1 thru 2 of s as text
on error
set ss to "0" & s as text
end try
------------------------------------------------------------------------
----------------------------
--Define some combinations
set mmddyy to mmonthIndex & dateDelimiter & dd & dateDelimiter & yy
as text
set mdyy to MonthIndex & dateDelimiter & d & dateDelimiter & yy as
text
set mdyyyy to MonthIndex & dateDelimiter & d & dateDelimiter & yyyy
as text
set hhmmss to hh & timeDelimiter & mm & timeDelimiter & ss as text
set hh24mmss to hh24 & timeDelimiter & mm & timeDelimiter & ss as text
set hms to H & timeDelimiter & m & timeDelimiter & s as text
set h24ms to h24 & timeDelimiter & m & timeDelimiter & s as text
set dateStamp to yy & mmonthIndex & dd as text
set timestamp to hh24 & mm & ss as text
set datetimeStamp to yy & mmonthIndex & dd & hh24 & mm & ss as text
-->>END
BODY:<<-----------------------------------------------------------------
----
--Define the results of the handler.
set theResult to {timestamp:timestamp, dateStamp:dateStamp,
datetimeStamp:datetimeStamp ,
, mdyy:mdyy, mmddyy:mmddyy, mdyyyy:mdyyyy ,
, hms:hms, h24ms:h24ms, hhmmss:hhmmss, hh24mmss:hh24mmss ,
, yyyy:yyyy, yy:yy, MonthIndex:MonthIndex, mmonthIndex:mmonthIndex,
monthText:monthText, abrMonthText:abrMonthText ,
, d:d, dd:dd, dayText:dayText, abrDayText:abrDayText ,
, H:H, hh:hh, h24:h24, hh24:hh24, m:m, mm:mm, s:s, ss:ss ,
, meridianValue:meridianValue}
-->>RETURN the results of the handler.<<
set AppleScript's text item delimiters to previousTID
return theResult
on error errorMessage number errorNumber partial result errorResult
from errorFrom to ErrorTo
try
set AppleScript's text item delimiters to previousTID
end try
error ("Handler '" & handlerName & "' " & errorMessage) number
errorNumber partial result errorResult from errorFrom to ErrorTo
end try --End Main body try wraper.
end DateAndTime
PS
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.