Forcing correct time format

I am writing a time-based task manager. The issue I’m running into is that the user inputs this information and may input “12” or “12:00” instead of “12:00:00” which is what I need to compare my time strings.

I’m currently using a variation of the following:


set timed1 to timedField1's stringValue()
set charCount to count characters of timed1
if charCount is less than 2 then
	set scheduledFor to (timed1 & ":00:00")
end if

This is a very primitive check that doesn’t account for anything other than what I assume the user may input.

I can’t be the first to tackle such an issue. Any suggestions?

Add a date formatter to the field in Interface Builder.

Ok, I’m trying to go a step further in this. I have put in a date picker so the user is forced to put in a valid time value (hour,minute,second).

I can get the string from the date picker using

set theHour to theComponents's hour() as string

and format/save it the way I want and it works perfectly.

So now I’m trying to figure out how to set the date picker’s time to a specific time.

I can use

set theDate to current application's class "NSDate"'s |date|()

to set it to the current date but I wish to set it to a custom time, not just the current time.

Thanks!

dateWithString: is the most straight forward way, but the string needs to be in a standard format,
for example, “2011-05-21 10:45:32 -0800”. You have to include all of that including theGMT offset. you can also use a date formatter in code to set the way you want to input your date and how you want it to look on output. For instance, the following code allows you to put in a date in the “5/21/11” format and get it out like the format above:

set inputFormatter to NSDateFormatter's alloc()'s init()
		inputFormatter's setDateFormat_("MM/dd/yy")
		set dateString to "05/21/11"
		log inputFormatter's dateFromString_(dateString)

The output (in my timezone) is “2011-05-21 00:00:00 -0700”

Ric

Ok, I’m back to the idea of using a date formatter. So how do I do error checking?

    on formatDate_(sender)
        set inputFormatter to current application's class "NSDateFormatter"'s alloc()'s init()
        inputFormatter's setDateFormat_("hh:mm:ss")
        set dateString to textField's stringValue()
        log inputFormatter's dateFromString_(dateString)
    end formatDate_

If the user doesn’t enter seconds, I’d rather it just fill in zeros instead of returning NULL on the whole dateString.

what’s about this


on formatDate_(sender)
	set dateString to textField's stringValue()
	set formatCounter to dateString's componentsSeparatedByString_(":")'s |count|()
	set inputFormatter to current application's class "NSDateFormatter"'s alloc()'s init()
	if formatCounter = 3 then
		inputFormatter's setDateFormat_("hh:mm:ss")
	else if formatCounter = 2 then
		inputFormatter's setDateFormat_("hh:mm")
	else
		log "wrong format"
	end if
	log inputFormatter's dateFromString_(dateString)
end formatDate__

Why did you get away from using the date picker? Using that will force the user to correctly input the date. If you want to set the date that appears in the date picker programmatically, you can use something like this:

dp's setDateValue_(current application's NSDate's dateWithString_("2011-05-21 10:45:32 -0700"))

Where dp is the IBOutlet for the date picker. It wasn’t clear to me whether you want to set this date once, or does it need to change every time the app is opened or what.

Ric

It’ll be used as a task timer basically. At this time, do this task. The end user can edit when they want it to happen.

I thought the date formatter would handle the formatting, but it doesn’t appear to do so in the way I thought it would. I may have to switch back to the picker. The reason I was switching back is because the picker requires a much larger amount of code to set the value, coerce the value to the format I need to work with my app.

Thanks for the help.