Working with NSDate objects- as readable AS values

Ok, as promised, here is a code sample that covers the major concepts in this thread that has made working with dates, and converting them back and forth to AS values as required, really quite easy in ASOC, thanks to Craig and Shane’s help above.

In the sample code all that is needed in IB is for a Date Picker(NSDatepicker) control to be placed in the window and connected (control-drag) to the app delegate class, selecting the DoWithTheDate_(sender) as the IB Action. Also connect FROM app delegate back to the datepicker selecting as the IB Outlet" datePicker " You’re done, then build and run. I suggest you follow along in the code as the dialog’s present the variations on the date you selected to, prevent “death by dialog”, or you can replace the Dislay Dialog command with log, and view the results in the Run Menu> Console.
I have also commented the code where appropriate. Note that there are no date comparision method, but they should be easy to cobble together with the key code from the above posts as well as from the code snippets in this sample.

Once again, thanks all for the help on this thread.

All the best

script TestAppDelegate
	property parent : class "NSObject"
	property datePicker : missing value
	property theDate : ""
	property theCal : ""
	property theComponents : ""
	property dateFormatter : ""
	property theString : ""
	on DoWithTheDate_(sender)
		--PART 1 - Changing the seconds in a date instance to zero
		-- set theDate property after user makes selection from the picker
		set theDate to my datePicker's dateValue()
		--set theCal var to a new(empty) instance of the calendar
		set theCal to current application's class "NSCalendar"'s currentCalendar()
		-- unpack the components of theDate to a variable
		set theComponents to theCal's components_fromDate_(254, theDate)
		-- Don't bother with seconds so set them to :00 (top of the clock)
		-- this will allow the user to pick a time to the minute
		-- for a task and prevent the task from firing at anytime 
		-- but on the top of the minute
		theComponents's setSecond_(0)
		-- reset theDate variable with the with new seconds we set above
		-- by rolling the components back up into a new NSDate
		set my theDate to theCal's dateFromComponents_(theComponents)
		-- Finally adjust the datePicker to the new value
		-- you should adjust the datepicker's attributes in IB to not show the seconds
		my datePicker's setDateValue_(theDate) --
		-- this dialog shows the resut from above with the seconds set to :00
		display dialog ("Part one - Result of changing seconds to :00  - Date is shown in International format   
		" & my datePicker's dateValue()'s |description| as string)
		-- END OF PART 1
		--PART 2 - Converting the NSDate to an AS  date and working with it
		-- Set new variables with values unpacked from theComponents 
		-- variable we worked with above and use them
		-- to make a new AS date
		set theYear to theComponents's |year|()
		set theMonth to theComponents's |month|()
		set theDay to theComponents's |day|()
		set theHour to theComponents's hour()
		set theMinute to theComponents's minute()
		set theSecond to theComponents's |second|()
		-- instantiate a new AS date to work with
		set theDate to current date
		display dialog ("PART 2 - Dialog 1 - This is the new AS date instance before revision 
		" & theDate as string)
		display dialog ("Part 2 - Dialog 2 - This is the AS Date's time string " & time string of (theDate))
		-- now revise theDate with our variables from above
		set day of theDate to 1 -- important
		set seconds of theDate to theSecond
		set year of theDate to theYear
		set month of theDate to theMonth
		set day of theDate to theDay
		set hours of theDate to theHour
		set minutes of theDate to theMinute
		display dialog ("PART 2 - Dialog 3 - This is the new AS date with Datepickers components and seconds revised  from above " & theDate as string)
		display dialog (("PART 2 - Dialog 4 - This is the value for the seconds from midnight for processing in AS if desired " & theDate's time) as string)
		-- PART 3 - Using a formatter to get a string value directly from an NSDate
		-- first set up a new variable
		set theString to theDate
		-- put a fresh NSDate instance from the datePicker into theDate replacing the AS date from PART 2
		set theDate to my datePicker's dateValue()
		-- Instantiate a dateFormatter. Note there is no formatter required in IB in the datepicker
		-- as we are creating it in the code
		set dateFormatter to current application's class "NSDateFormatter"'s alloc()'s init()
		-- set the dateformatter's attributes. Play with them to change the
		-- values in the dialog.
		dateFormatter's setDateStyle_(3) -- 0 = none, 1 = short, 2 = med, 3 = long, 4 = full
		dateFormatter's setTimeStyle_(4) -- 0 = none, 1 = short, 2 = med, 3 = long, 4 = full
		-- use the dateFormatter's get method to get a string from theDate
		set theString to dateFormatter's stringFromDate_(theDate)
		display dialog ("PART 3 - a nicely formatted date string for presentation to the user -
		" & theString as string)
	end DoWithTheDate_
	on applicationWillFinishLaunching_(aNotification)
		-- set the datePicker to now so the user doesn't see a strange date in the picker
		set theDate to current application's class "NSDate"'s |date|()
		--initialize the date property we will be working with
		my datePicker's setDateValue_(theDate)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return true
	end applicationShouldTerminate_
end script

northbridge -

Thank you for your sample code! By combining your code with other bits, it was really useful to my project. Since it wasn’t super obvious to me at first, I’d like to contribute a handler that converts a UTC date to an NSDate object. I really want to “give back” to this great forum - so hopefully it helps someone out. Thanks again.

to convertUtcToAsDate(x)
		tell x to set {y, m, d, h, mn, s} to {text 1 thru 4, text 6 thru 7, text 9 thru 10, text 12 thru 13, text 15 thru 16, text 18 thru 19}
		set theCal to current application's class "NSCalendar"'s currentCalendar()
		set theComponents to theCal's components_fromDate_(254, theDate)
		theComponents's setYear_(y as integer)
		theComponents's setMonth_(m as integer)
		theComponents's setDay_(d as integer)
		theComponents's setHour_(h as integer)
		theComponents's setMinute_(mn as integer)
		theComponents's setSecond_(s as integer)
		set theDate to theCal's dateFromComponents_(theComponents)
		return theDate
end convertUtcToAsDate