Six digit numeric date string creator

Previously I posted an error checking mechanism¹ because NSCalendar was occasionally returning garbage. Well, unfortunately it didn’t fix the error.

So I’ve written a simpler and (hopefully) more reliable way to create a 6 digit numeric date string.

use framework "Foundation"
use scripting additions

sixDigitDateCode()

on sixDigitDateCode()
	set dateFormatter to current application's NSDateFormatter's alloc()'s init()
	set todaysDate to current application's NSDate's |date|()
	set codes to {"MM", "d", "Y"}
	set dateStr to ""
	repeat with code in codes
		(dateFormatter's setLocalizedDateFormatFromTemplate:code)
		set dateComponent to (dateFormatter's stringFromDate:todaysDate) as text
		if (count of characters of dateComponent) = 4 then
			set dateComponent to characters 3 thru 4 of dateComponent
		else if (count of characters of dateComponent) = 1 then
			set dateComponent to "0" & dateComponent
		end if
		set dateStr to dateStr & dateComponent
	end repeat
	return dateStr
end sixDigitDateCode

¹"Find item occurring most frequently in a list": http://macscripter.net/viewtopic.php?id=45186

Easier

on sixDigitDateCode()
	set dateFormatter to current application's NSDateFormatter's alloc()'s init()
	set todaysDate to current application's NSDate's |date|()
	set codes to {"MM", "dd", "yy"}
	set dateStr to ""
	repeat with code in codes
		(dateFormatter's setLocalizedDateFormatFromTemplate:code)
		set dateComponent to (dateFormatter's stringFromDate:todaysDate) as text
		set dateStr to dateStr & dateComponent
	end repeat
	return dateStr
end sixDigitDateCode

Still easier

on sixDigitDateCode()
	set dateFormatter to current application's NSDateFormatter's alloc()'s init()
	set todaysDate to current application's NSDate's |date|()
	set dateFormatter's dateFormat to "MMddyy"
	return (dateFormatter's stringFromDate:todaysDate) as text
end sixDigitDateCode

lowercase y is crucial because uppercase Y represents year in “Week of Year” based calendars.

And of course:

sixDigitDateCode()

on sixDigitDateCode()
	using terms from scripting additions
		tell (current date) to return text 2 thru -1 of ((1000000 + ((its month) * 10000) + (its day) * 100 + (its year) mod 100) as text)
	end using terms from
end sixDigitDateCode

Can you explain how/where this was happening? It sounds an odd thing.

I guess you need such a thing, but anyone with memories of the Y2K debacle will suggest such things are far better avoided.

Thanks for making this significantly simpler.

I was using NSCalendar to build a path to a folder containing the 6 digit numeric date string in it’s name (i.e. “/volumes/path/to/server/folder/101416_assets”). Occasionally (once every 200th time used) it would create the path with an extra path component at the end (i.e. “/volumes/path/to/server/folder/1014/16_assets”). The error would always occur before the 2 digit year.

Not too worried about this. The folders eventually get put in subfolders with a four-digit year in the name.

set dateFormatter's dateFormat to "MMddyy"

btw I didn’t know you could set class properties in this way in ASOC. Is this new?

If you can be bothered, I’d love to see the code.

It’s not new – but IMO it’s not a particularly good idea. For one thing, it doesn’t always work; for another, there can be subtle class issues, especially if you also use it to retrieve properties. It essentially uses key-value coding, which bypasses the scripting bridge’s normal way of converting to and from AppleScript and Objective-C based on signatures and property definitions.

Most easiest (when AppleScript Toolbox is installed)?

AST format date "MMddYY"

So would this be better, or equally undesirable?

dateFormatter's setDateFormat:("MMddyy")

I apologize for too much translating one-to-one from ObjC :wink:

That’s what I’d use (maybe without the parentheses, but that’s probably just a typing thing).

This is the problematic NSCalendar version. Note: I haven’t yet used the NSDateFormatter version enough to determine if I get the same error.

use framework "Foundation"
use scripting additions
set serverPath to "/Volumes/Path/To/Server/Folder/" & sixDigitDateStr() & "_assets"
on sixDigitDateStr()
	set todaysDate to current application's NSDate's |date|()
	set cal to current application's NSCalendar's currentCalendar()
	set calComponents to cal's components:254 fromDate:todaysDate
	set |month| to adjustComp(calComponents's |month|() as text)
	set |day| to adjustComp(calComponents's |day|() as text)
	set |year| to adjustComp(calComponents's |year|() as text)
	return |month| & |day| & |year|
end sixDigitDateStr
on adjustComp(dateComp)
	if length of dateComp = 1 then
		set dateComp to "0" & dateComp
	else if length of dateComp = 4 then
		set dateComp to characters 3 thru 4 of dateComp as text
	end if
	return dateComp
end adjustComp

Are you sure the intermittent error you were getting wasn’t a slash between the two year digits?

You have this line:

That makes a two-character list, and then coerces it to a string. What you end up with depends on the value of text item delimiters at the time; if you’ve set them to a slash somewhere else in your code, without resetting them to the default, you’d get a slash inserted between the digits.

The better way to extract a substring is:

       set dateComp to text 3 thru 4 of dateComp 

which is also more efficient, because there’s no list created.

That’s almost certainly what was happening. Thanks.