• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Date Help - D&T stamp w Short dates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Date Help - D&T stamp w Short dates


  • Subject: Re: Date Help - D&T stamp w Short dates
  • From: "Daniel A. Shockley" <email@hidden>
  • Date: Tue, 7 Jan 2003 17:51:14 -0500

> Basically, I would like to create a date - time stamp that ideally would
look like the following

IDEAL1: 01-06-03 09-23-21 AM (mm-dd-yy)
> IDEAL2: 2003-01-06 09-23-21 AM (yyyy-mm-dd)

I keep forgetting to optimize this handler:
set ideal1 to DateToString( "mm-dd-yy hh-MM-ss ?m", current date )
set ideal2 to DateToString( "yyyy-mm-dd hh-MM-ss ?m", current date )

Hmm, I've got a simpler (faster) format-date handler, which cannot give you names of months or days, but it's pretty fast, just doing a bunch of replaces, basically. Mine also does not have an escape character. I did add Arthur's idea of an am/pm option, as well as trying to coerce the date parameter to date, if possible, or if not, just using the current date:


dateAsCustomString("12/25/2002 1:30 PM", "YYYY MM DD hh mm ss ap")
--> "2002 12 25 01 30 00 pm"

dateAsCustomString("1/6/2003 9:23:21 AM", "YYYY-MM-DD hh-mm-ss AP")
--> "2003-01-06 09-23-21 AM"

on dateAsCustomString(incomingDate, stringFormat)
-- version 1.2, Daniel A. Shockley http://www.danshockley.com
-- 1.2 added am/pm option, and date class checking
-- with a nod to Arthur J. Knapp
-- NEEDS getMonthNum(), simpleReplace()

-- takes any form of MM, DD, YYYY, YY
-- AND any form of hh, mm, ss
-- (optional ap or AP, which gives am/pm or AM/PM)
-- MUST USE LOWER-CASE for TIME!!!! (avoids month/minute conflict)

-- use single letters to not force 2 digits

-- textVars are each always 2 digits, whereas month and day
-- may be 1 digit, and year will normally be 4 digit

if class of incomingDate is not date then
try
set incomingDate to date incomingDate
on error
set incomingDate to (current date)
end try
end if

set numHours to (time of incomingDate) div hours
set textHours to text -2 through -1 of ("0" & (numHours as string))

set numMinutes to (time of incomingDate) mod hours div minutes
set textMinutes to text -2 through -1 of ("0" & (numMinutes as string))

set numSeconds to (time of incomingDate) mod minutes
set textSeconds to text -2 through -1 of ("0" & (numSeconds as string))

set numDay to day of incomingDate as number
set textDay to text -2 through -1 of ("0" & (numDay as string))

set numYear to year of incomingDate as number
set textYear to text -2 through -1 of (numYear as string)

-- Emmanuel Levy's Plain Vanilla get month number function
copy incomingDate to b
set the month of b to January
set numMonth to (1 + (incomingDate - b + 1314864) div 2629728)
set textMonth to text -2 through -1 of ("0" & (numMonth as string))

set customDateString to stringFormat

if numHours > 12 and (customDateString contains "ap" or customDateString contains "AP") then
-- (afternoon) and requested am/pm
set numHours to numHours - 12 -- pull off the military 12 hours for pm hours
set textHours to text -2 through -1 of ("0" & (numHours as string))
end if

set customDateString to simpleReplace(customDateString, "MM", textMonth)
set customDateString to simpleReplace(customDateString, "DD", textDay)
set customDateString to simpleReplace(customDateString, "YYYY", numYear as string)
set customDateString to simpleReplace(customDateString, "hh", textHours)

set customDateString to simpleReplace(customDateString, "mm", textMinutes)
set customDateString to simpleReplace(customDateString, "ss", textSeconds)


-- shorter options
set customDateString to simpleReplace(customDateString, "M", numMonth)
set customDateString to simpleReplace(customDateString, "D", numDay)
set customDateString to simpleReplace(customDateString, "YY", textYear)
set customDateString to simpleReplace(customDateString, "h", numHours)
set customDateString to simpleReplace(customDateString, "m", numMinutes)
set customDateString to simpleReplace(customDateString, "s", numSeconds)

-- AM/PM MUST be after Minutes/Month done, since it adds an M
if (time of incomingDate) > (12 * hours) then
-- afternoon
set customDateString to simpleReplace(customDateString, "ap", "pm")
set customDateString to simpleReplace(customDateString, "AP", "PM")
else
set customDateString to simpleReplace(customDateString, "ap", "am")
set customDateString to simpleReplace(customDateString, "AP", "AM")
end if

return customDateString

end dateAsCustomString


on simpleReplace(thisText, oldChars, newChars)
-- version 1.1

-- 1.1 coerces the newChars to a STRING, since other data types do not always coerce
-- (example, replacing "nine" with 9 as number replaces with "")
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to the oldChars
set the parsedList to every text item of thisText
set AppleScript's text item delimiters to the {(newChars as string)}
set the newText to the parsedList as string
set AppleScript's text item delimiters to oldDelims
return newText

end simpleReplace

--
----
Daniel A. Shockley
email@hidden
email@hidden
http://www.danshockley.com
_______________________________________________
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.

  • Follow-Ups:
    • Re: Date Help - D&T stamp w Short dates
      • From: Harald E Brandt <email@hidden>
  • Prev by Date: Re: Forcing 'tell app' to use carbon, rather than classic app
  • Next by Date: Re: GUI Scripting and "International" Preferences
  • Previous by thread: Re: Date Help - D&T stamp w Short dates
  • Next by thread: Re: Date Help - D&T stamp w Short dates
  • Index(es):
    • Date
    • Thread