Convert Months less than 10 to two digits

I need help on using this command to convert months 1-9 to two digits so it would be 01-09

set {year:y, month:m, day:d} to (current date)
set theDate to (y * 10000 + m * 100 + d) as string

Any help is greatful

Thanks

-tribunescripter

One way:

do shell script "date +'%Y%m%d'"

I am sure others will post an AppleScript solution.

Cheers,

Craig

I just ran your script and got this for an answer:

“20080902”

What’s wrong with that? Today is Sept. 2, 2008.

Nothing is wrong with that.

2008 => year, 09 => month, and 02 => day.

%Y is year in 4 digit format
%m is month in 2 digit format
%d is day in 2 digit format

To make it read 09-02-2008 you would use %m-%d-%Y

Cheers,

Craig

If you want a pure AppleScript method (where you can see what’s going on)

set d to short date string of (current date)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set tParts to text items of d
repeat with k from 1 to 3
	set item k of tParts to pad(item k of tParts, 2)
end repeat
set AppleScript's text item delimiters to "/" -- or any other symbol you like
set tDate to tParts as text
set AppleScript's text item delimiters to tid
tDate --> "09/03/08"

on pad(num, howLong)
	set c to count num
	repeat howLong - c times
		set num to "0" & num
	end repeat
	return num
end pad

Thanks, I learnt something.
I thought that repeat x times would fail if x is a negative number.
AppleScript is fair enough to take care of that.

Yvan KOENIG (from FRANCE mercredi 3 septembre 2008 20:51:35)

Oops! I see now that you wanted to shorten the year to two digits.

Adam,
No need to loop in pad:

on pad(num, howLong)
	set num to text -howLong thru -1 of ("00" & num)
	return num
end pad

Since StefanK hasn’t piped-in yet, he helped me with this DateStamp generator. I use it to give YYMMDD stamps that sort nicely in the Finder on filenames. :wink:

-- Date stamp generator
--
-- with help from StefanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?id=20420
--
on dateStamp()
	
	-- Load date components from system
	tell (current date)
		set dayStamp to day
		set monthStamp to (its month as integer)
		set yearStamp to year
	end tell
	
	--coerce components to two-digit form
	set dayStamp to (text -2 thru -1 of ("0" & dayStamp as text))
	set monthStamp to (text -2 thru -1 of ("0" & monthStamp as text))
	set yearStamp to (text 3 thru 4 of (yearStamp as text))
	
	--Assemble datestamp
	return yearStamp & monthStamp & dayStamp as text
	
end dateStamp

This is “outdated”. Now I’d prefer Craig’s way

i.e., Finder sortable date stamp (two digits for each of year, month, day):

do shell script "date +'%y%m%d'"

and to add punctuation (dash here)

do shell script "date +'%y-%m-%d'"

or, if you prefer an AppleScript version (Nigel Garvey’s cleanup of a date stamp by Kai Edwards)

tell (current date) to tell (year * 10000 + (its month) * 100 + day) as string to text 3 thru 4 & text 5 thru 6 & text 7 thru 8 --> 080904, a Finder sortable date string.

It’s easy to add punctuation to the string if you want it by inserting it between the text range statements:

tell (current date) to tell (year * 10000 + (its month) * 100 + day) as string to text 3 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 --> 08-09-04, a dash-separated Finder sortable date string.

LOL…I have mixed feelings about using shell stuff. Starts to feel like AppleScript is just a handy way to string “do shell” commands together. :stuck_out_tongue_winking_eye:

I personally prefer to use AppleScript itself unless there is some huge speed reason to use shell, or there just isn’t any other handy way to do things (or AppleScript causes some undue complexity/confusion). But that’s just me. I suppose if I was more “hardcore” or doing something more commercially-oriented, I might change my tune. :wink: I like your “outdated” one just fine, it’s more concise than my original one was and also is a good reminder/trainer for using text manipulation.

Ah. That’s actually an obfuscatory one-lining by Kai Edwards of a short-date generator by Nigel Garvey. :rolleyes:

The idea is that (ignoring the month-to-integer coercion) four math operations, one number-to-string coercion, three text extractions, and two text concatenations are more efficient than three number-to-string coercions, three text extractions, and four text concatenations, which is what a vanilla solution would involve otherwise:

tell (current date) to text 3 thru 4 of (year as string) & text -2 thru -1 of ("0" & ((its month) as integer)) & text -2 thru -1 of ("0" & day)

:slight_smile:

Ahh, age creapeth up – got it backwards :o

…and a patrridge in a pear tree…wow.

Okay, so make that Nigel and Kai as the one-liner fun-makers. :wink:

So I’ve always heard that the do shell script is slower than the applescript method, since it has to open an instance of the shell before processing the date…

but what kind of “slowdown” are we talking? Is it really any significant amount?

approx. 30 - 50 ms per call