KDeogharkar
KDeogharkar

Reputation: 10959

Convert NSString to NSDate returns (null) value

I have one Date in string format "2013-03-19T19:00:50"
I am trying to convert it into NSDate using NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-ddThh:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(@"date in date format : %@",startDate);

but it is giving me null date

date in date format : (null)

What is the issue?

Upvotes: 0

Views: 1276

Answers (3)

Venk
Venk

Reputation: 5955

Do like this,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(@"date in date format : %@",startDate);

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90127

According to the date formatter patterns hh means Hour [1-12] You want Hour [0-23] which is HH.

And any letters that are not date format patterns, or must not be interpreted in this way have to be put in between apostrophes.

use [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

Use :

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

You have time in 24 hour format, so HH is required. hh is used when time is in 12 hour format.

And T is required to be in single quote, T is not a part of date this is an added text on it.

Upvotes: 7

Related Questions