How can I convert time string (in 24h format) into NSDate for comparison?
I get nil and wrong NSDate here:
let a = "5:46"
let b = "24:30"
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
dateFormatter.dateFormat = "k:mm" // k = Hour in 1~24, mm = Minute
let outputA = dateFormatter.dateFromString(a)
let outputB = dateFormatter.dateFromString(b)
println("A: \(outputA) B: \(outputB)")
Then I get
A: Optional(1999-12-31 20:46:00 +0000) B: nil
when I wish to get
A: 5:46 AM B: 00:30 AM
or
A: 05:46 B: 24:30
It doesn't really matter which NSDate format as long as I can compare those two hours.
Add
NSTimeZone
in date formate.Output is :
There are three completely different questions here:
Why does the first date look wrong?
You are getting
20:46
for the first date because5:46am
in your local time zone is equal to20:46 GMT
(that's what the+0000
means, GMT+0). So don't worry about that it looks different. The simple truth is that 5:46am in your local timezone is 20:46 GMT+0.People frequently recommend specifying the formatter's
timeZone
to GMT/UTC. In my opinion, that's a fundamentally flawed approach. That works, but it suggests a misunderstanding of the underlying issue, thatNSDate
objects do not have an inherent concept timezone. If you want to show a time in a current time zone, you always use a formatter. I'm presuming that when you say 5:46am, you mean 5:46am where the user actually is located, rather than 5:46m in London.Clearly, if when you say 5:46, you really meant 5:46 GMT (i.e. in London), then fine, set the timezone to be GMT. You'll do that a lot with Internet date formats like RFC 3339 or ISO 8601, where we do consciously change everything to GMT for consistency's sake. But if that's not the case, I think it's better to let the formatter use the default
timeZone
(your current timezone) and just be aware thatNSLog
/print
will always show the time in GMT and know that when you want to show the time in the app, always use a formatter to properly format it for your local time zone.Why is the second date
nil
?This one is a bit of a mystery. Your formatter is wrong (you should use either
dateFormat
ortimeStyle
/dateStyle
, but not both), but I don't think that's the problem. The use of24:30
is a little curious to US eyes (we'd generally use00:30
), but I guess its not unheard of in UK, China, Japan, and Hong Kong, etc. Plus you're usingk:mm
, so I would have thought that it would have accepted24:30
. Thek:mm
formatter string worked fine for me, though.Bottom line I cannot reproduce the
nil
behavior you describe. Perhaps you can tell us what locale you're using and use justdateFormat
, but nottimeStyle
.Why are the output times not formatted properly?
If, having parsed the original strings to
NSDate
objects, if you want to create a new output string in some predetermined format, then you'd use another formatter for converting thatNSDate
back into a string in the desired format.On my computer, that outputs:
The dates are those times are correctly parsed into
NSDate
objects (which output their value in GMT,+0000
) and then reconverted to output strings per whatever style/format you provide.Clearly, this assumes you fix the issue with
dateB
beingnil
. Please provide more information regarding your configuration and/or provide us a MCVE. But I cannot reproduce the behavior you describe.you can try the below code for the your problem.
Note: If you are using 24 hour time format then u don't need to take "24:30" and NSDate object will never give you only time. it will always return date also if you will use the format which i mentioned in above code.