views:

81

answers:

2

Is it possible to see on a NSDate whether the time part has been set or not?

Or to rephrase the question: is it possible to create a NSDate in such a way that when I look at the instance I can see whether the time was set or not?

I am having an issue where I read sometimes DATE and sometimes DATETIME from a database. In both cases I create a NSDate object however I would like to know what the original type was. I know I can do a wrapper or something around the NSDate but I just wondering if there is some other simpler way by just looking at the NSDate instance to see whether the time was set or not originally?

tia

+3  A: 

I think you are creating your NSDate instances the same way, just with hours, minutes and seconds set to 0 in the DATE only case, converting that to a NSTimeInterval and using that to initialize your date?

Then, there is no way to distinguish a real date and a false one. You can check using the same algorithm you used to create it, i.e. are hours == 0 && minutes == 0 && seconds == 0. But this will fail for a real DATETIME with those properties - which might or might not be a problem.

As far as documentation tells, there is no way to set a date only, so your convertion already introduces this loss of detail.

Eiko
thanks that answered my question.
Anders K.
+2  A: 

An NSDate represents a single point in time. As such, it will always have a time associated with it. Therefore, your question is invalid. If you have an NSDate, then it has a time component. If you want to extract various portions of the date (such as the month, year, hour, second, etc), then you'll use -[NSCalendar components:fromDate:].

Dave DeLong