• 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: Time Stamp String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Time Stamp String


  • Subject: Re: Time Stamp String
  • From: kai <email@hidden>
  • Date: Mon, 14 Mar 2005 22:29:31 +0000


On Mon, 14 Mar 2005 12:55:08 -0800, Todd Geist wrote:

I wanted to create a routine that would take the current date and return it
in the form


yyyymmdd_time

Ex

20050314_46211

I created this script which works just fine but I was wondering if there was
a better way.



_____________________________ on TimeStampString(theDate) set theTS to theDate

    set tid to text item delimiters
    set text item delimiters to ""

set theYear to the year of theTS
set theDay to day of theTS as text
--pad the day number to 2 digits
set theDay to "0" & theDay as text
set theDay to items ((count of theDay) - 1) through (count of theDay) of
theDay


    set theMonth to the month of theTS as number
    --pad the month number to 2 digits
    set theMonth to "0" & theMonth as text
    set theMonth to items ((count of theMonth) - 1) through (count of
theMonth) of theMonth

set theTime to time of theTS
set theFullString to theYear & theMonth & theDay & "_" & theTime as text
return theFullString


    set text item delimiters to tid

end TimeStampString
_____________________________

Don't forget that you can take advantage of AppleScript's implicit coercions when concatenating. For example, if you concatenate a string (on the left) and a number (on the right), the number is automatically coerced to a string: "0" & 1 --> "01".


That should abbreviate things a little:

---------

on pad(n)
("0" & n)'s text -2 thru -1
end pad
set d to current date -- or whatever
set d to (d's year as string) & pad(d's month as integer) & pad(d's day) & "_" & d's time


---------

If speed is a serious consideration, then the following 'pad' handler may be slightly faster:

---------

on pad(n)
	(100 + n as string)'s text -2 thru -1
end pad

---------

---
kai

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: which app?
  • Next by Date: Re: Time Stamp String
  • Previous by thread: Re: Time Stamp String
  • Next by thread: Re: Time Stamp String
  • Index(es):
    • Date
    • Thread