Help with date format

I have no AS knowledge so please sorry if this is too basic…
I’m trying to change a script I downloaded, but can’t seem to understand how to format the date to a specific format like this:
2022-05-12, 11.17.24 PM
(note the dashes on the date and the dots on the time)

I would like to set a variable with that format so I can use it later on.

This is the original code:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:" -- EDITED
-- set notesBackupsFolder to rootFolder & "Notes Backups:"
set currentDate to (current date) as text

tell application "Finder"
	-- try
	-- 	folder notesBackupsFolder
	-- on error
	-- 	make new folder at folder rootFolder with properties {name:"Notes Backups"}
	-- end try
	set exportFolder to make new folder at folder rootFolder with properties {name:currentDate}
	set exportFolder to exportFolder as text -- THIS, ADDED
end tell

Thank you! :slight_smile:

Here is a pure AppleScript snippet that will convert a date variable into a string in the format you want

set cDate to (current date)
set text item delimiters to {".", ":"}
set dateString to (year of cDate as text) & "-" & (text -2 thru -1 of ("0" & ((offset of (text 1 thru 3 of ((month of cDate) as text)) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1))) & "-" & (day of cDate) & ", " & ((text items of (time string of cDate)) as text)

-- dateString is a variable that contains the date in string format

Hi Robert,

Thank you for the script.
I’m just a bit confused where to put that? As I mentioned, I have no knowledge when it comes to AS.
Can you send me both or codes merged into one so I can use it without making a mess?

Thanks! :slight_smile:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:" -- EDITED
-- set notesBackupsFolder to rootFolder & "Notes Backups:"
set cDate to (current date)
set text item delimiters to {".", ":"}
set dateString to (year of cDate as text) & "-" & (text -2 thru -1 of ("0" & ((offset of (text 1 thru 3 of ((month of cDate) as text)) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1))) & "-" & (day of cDate) & ", " & ((text items of (time string of cDate)) as text)


tell application "Finder"
	-- try
	-- 	folder notesBackupsFolder
	-- on error
	-- 	make new folder at folder rootFolder with properties {name:"Notes Backups"}
	-- end try
	set exportFolder to make new folder at folder rootFolder with properties {name:dateString}
	set exportFolder to exportFolder as text -- THIS, ADDED
end tell

Maybe something like this will work for you?

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:"
set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

tell application "Finder"
	set exportFolder to make new folder at folder ¬
		rootFolder with properties {name:currentDate}
	set exportFolder to exportFolder as text
end tell

wch1zpink, that did the trick, thank you :slight_smile:

On my original post I forgot to add the text to the format, because I just focused on the date format. What I mean is that the final folder’s name should be something like this:
Backup, 2022-05-12, 11.17.24 PM

I was looking at the script and tried something that worked. Can you please confirm that this is the right way to do it and I will not run into issues in the future?

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:"
set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

tell application "Finder"
	set exportFolder to make new folder at folder rootFolder with properties {name:"Backup, " & currentDate}
	set exportFolder to exportFolder as text
end tell

Thank you Robert!
The solution provided by wch1zpink seems to work and uses less code.
I appreciate your time and help! :slight_smile:
AppleScript is indeed such a complex language

It depends how complex you want to make it. :wink: Robert’s vanilla method for the date string can be simplified and made correct for days < 10 like this:

set {year:y, month:m, day:d, time string:t} to (current date)
set text item delimiters to {".", ":"}
set dateString to (y as text) & "-" & (text -2 thru -1 of ("0" & (m as integer))) & "-" & (text -2 thru -1 of ("0" & d)) & ", " & ((text items of t) as text)

However, for efficiency, and bearing in mind that the contents of a date’s ‘date string’ depend on the user preferences set on the running machine, and to leave the TIDs undisturbed, I must admit that my own vanilla solution would be something like this:

set {year:y, month:m, day:d, hours:hr, minutes:min, seconds:sec} to (current date)
tell (y * 10000 + m * 100 + d) as text to ¬
	set dateStr to text 1 thru 4 & "-" & (text 5 thru 6 & "-" & text 7 thru 8)
tell (1000000 + hr * 10000 + min * 100 + sec) as text to ¬
	set timeStr to text 2 thru 3 & "." &( text 4 thru 5 & "." & text 6 thru 7)
set dateString to dateStr & ", " & timeStr

The shell script’s certainly less code. :slight_smile:

Wow!

I did not know you could coerce a month to an integer.
I’ve been doing it the hard way for a long time.

I learn something new every day.

Thanks Nigel

Hi Robert.

It’s been possible to coerce AppleScript months to integer since Mac OS X 10.3. The hard way dates from sometime before that! :slight_smile:

Between the two, Emmanuel Lévy came up with what came to be known as “French Vanilla”:

on monthAsInteger(theDate)
	copy theDate to b
	set month of b to January
	return 1 + (theDate - b + 1314864) div 2629728
end monthAsInteger

monthAsInteger(current date)

… which someone else later reduced to:

on monthAsInteger(theDate)
	copy theDate to b
	set b's month to January
	return (theDate - b + 2500000) div 2500000
end monthAsInteger

monthAsInteger(current date)

Your edits certainly are a valid solution. Sometimes there is no “right or wrong” approach.

Another perfectly valid option would be to just add "Backup, " to:

[format]set currentDate to do shell script “/bin/date "+%Y-%m-%d, %I.%M.%S %p"”[/format]
Like this:
[format]set currentDate to do shell script “/bin/date "+Backup, %Y-%m-%d, %I.%M.%S %p"”[/format]

You can also bypass all of that other code and just use this one line instead.

do shell script "cd ~ ;mkdir -p \"My Files/Backups/Notes/$(date \"+Backups, %Y-%m-%d, %I.%M.%S %p\")\""

OR if you need to set the exportFolder variable, this line of code will do everything.

set exportFolder to (do shell script "cd ~ ;f=$(date \"+Backups, %Y-%m-%d, %I.%M.%S %p\") ;mkdir -p \"My Files/Backups/Notes/$f\" ;echo \"$PWD/My Files/Backups/Notes/$f\"") as POSIX file as text

Sorry for the late reply.
Thank you for confirming that my approach was good enough to achieve it and thank you for the other options! There is, indeed, more than one way to approach it :wink: