date format returned from plist

I’m having trouble doing date calculations with the results of defaults read shell script
the plist has a date format of 1/29/07 but the result i get back is this

2007-01-29 12:00:00 -0500

and help with this would be greatly appreciated

thanks
mm

This seems to return an AppleScript date object:

on parseDefaultsDate(someText)
	set monthList to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
	set someDate to ""
	set ASTID to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to " "
		set someTime to second text item of someText
		set someText to first text item of someText
		
		set AppleScript's text item delimiters to "-"
		tell someText's text items to set {someYear, someMonth, someDay} to {first item, second item, third item}
		
		set AppleScript's text item delimiters to ""
		set someMonth to monthList's item (someMonth as integer)
		
		set someDate to date (someDay & space & someMonth & space & someYear & space & someTime)
	end try
	set AppleScript's text item delimiters to ASTID
	return someDate
end parseDefaultsDate

Bruce,

thanks allot that works perfect I’m not sure why but tryed formatting the date from shell like this

defaults read “directory and file goes here”| date ‘+%A, %B %e, %Y %I:%M:%S %p’ but that didn’t work

Hi Bruce do you need the monthlist

set someText to "2007-01-29 12:00:00 -0500"
my parseDefaultsDate(someText)
on parseDefaultsDate(someText)
	set someYear to word 1 of someText
	set someMonth to word 2 of someText
	set someDay to word 3 of someText
	set someTime to word 4 of someText & ":" & word 5 of someText & ":" & word 6 of someText as string
	set someDate to (someDay & space & someMonth & space & someYear & space & someTime)
	set someDate to date someDate
end parseDefaultsDate

I still call myself a novice when it comes to shell, and applescript for that matter.
But it would seem you are trying to read a plist and then pipe ALL of the info returned to the date command.
This will not work the date command will not understand the input your are sending it or for that matter even be able to receive it let alone parse it.

There is probably a way to pass the date on to date. But Bruce’s script is much easier.

Some caveats:

Using the month names, as Bruce does, presumes that the language is English.

Using the month numbers, as Mark does, presumes that the default date for the machine is dd/mm/yyyy, whereas the default for US Machines is mm/dd/yyyy.

In case it wasn’t obvious, the -0500 at the end signifies Eastern Standard Time. It’s the time shift from GMT.

I used that because numbers weren’t working, but I just realized why (thanks, Adam).

See above. :slight_smile:

Additionally, all of this might break for someone who is not using a Gregorian calendar.

Edit:

I have to nitpick this. What is the point of the as string coercion at the end?

A constant problem for Canadians who often use US as their language of choice, but vary wildly in date formats between British and US formats.

And using ‘words’ to break up the text also presumes that the language is English. (‘Words’ are defined differently in AppleScript according to the user’s language.) Obviously, English is mm’s preferred language, but for general use, it would be a safer (and slightly faster) idea to begin with a compiled date and change its properties. I did the version below for my own amusement this morning. It should work with any language and with any system back as far as OS 8.0. It also allows the option of translating the shift amount to your own time zone or not.

on parseDefaultsDate from someText given shifting:shifting
	set someDate to date "1 1 1"
	set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
	set astid to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to space
	set {datePart, timePart, shiftPart} to someText's text items
	
	set AppleScript's text item delimiters to "-"
	tell datePart's text items to set {someDate's year, someDate's month, someDate's day} to {beginning, item (item 2) of monthList, end}
	
	set AppleScript's text item delimiters to ":"
	tell timePart's text items to set someDate's time to beginning * hours + (item 2) * minutes + end
	
	set AppleScript's text item delimiters to astid
	
	if (shifting) then
		return someDate - (shiftPart div 100 * hours + shiftPart mod 100 * minutes - (my (time to GMT)))
	else
		return someDate
	end if
end parseDefaultsDate

parseDefaultsDate from "2007-01-29 12:00:00 -0500" without shifting
--> date "Monday 29 January 2007 12:00:00" -- On my machine (UK).

parseDefaultsDate from "2007-01-29 12:00:00 -0500" with shifting
--> date "Monday 29 January 2007 17:00:00" -- UK currently on GMT.

Excellent ideas Nigel, as always. :cool:

Another script gets filed away with a basename postfix = (NG). :wink:

thanks to all for there imput

nitpick away, Its the only way I am going to learn. :slight_smile:
I was most likely trying something else and left it in or also most likely bad habit (coercion does my head in) which in both cases is a Bad habit.

I actually assumed that some who used it would switch the : someDay, someMonth,someYear around to what they needed.
set someDate to (someMonth & space & someDay & space & someYear & space & someTime)

I should explain more.