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?
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.
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"];
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);
Related
"2014-02-03T23:10:00Z"
I'm aware of what each of the numbers represents, but the T and Z throw me off. Does anyone know what type of date format this is? I'd rather use a native parser if I could find one.
Thanks
It is an ISO 8601 format (http://en.wikipedia.org/wiki/ISO_8601).
T is Time delimeter while Z is used if time zone is UTC.
It appears to be ISO 8601 format. RestKit has a parser for it. You should also be able to get NSDateFormatter to parse it.
"2014-02-03T23:10:00Z" is the ISO 8601 format (http://www.w3.org/TR/NOTE-datetime)
the T represents the beginning of the time and the Z represents that the date is expressed in UTC (Coordinated Universal Time)
if you want to get the date with a specific format you could use NSDateFormatter
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd"];
NSString *stringDate = [dateFormatter stringFromDate:date];
NSLog(#"%#", stringDate); // 2014/03/03
If you need just a certain part of the date (e.g just the current month):
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM"];
NSInteger *monthNumber = [[dateFormatter stringFromDate:date] integerValue];
NSLog(#"%i", monthNumber); // 03
If you need the date in a specific language:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd' de 'MMMM' del 'yyyy"]; // You can add your own text between single quotes
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es_ES"]];
NSString *stringDate = [dateFormatter stringFromDate:date];
NSLog(#"%#", stringDate); // 03 de febrero del 2014
I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.
I would like to create an NSDate from a string:
NSString *mostRecentMentionMessageTimestamp = [mostRecentMessage valueForKey:#"updated_at"];
NSLog(#"%#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"%#",mostRecentMentionDate);
When I print my string it is:
2011-11-10T07:22:59Z
After I make a date from string and print it, it is (null)
What am I doing wrong?
You need to set the time zone and drop the Z at the end of the date format. Since your date has a Z, it is in UTC. So you can use:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss"];
The format is almost correct, use:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
to solve the time zone issue use the solution by #Jef add:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
Full example code:
NSString *mostRecentMentionMessageTimestamp = #"2011-11-10T07:22:59Z";
NSLog(#"input: %#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"output: %#",mostRecentMentionDate);
NSLog output:
input: 2011-11-10T07:22:59Z
output: 2011-11-10 07:22:59 +0000
the string value which you are trying to convert to NSDate is incorrect.
"2011-11-10T07:22:59Z"
it should have a timezone difference value in place of "Z" to make it compatible to date formatter's format.
e.g. 2011-11-10T07:22:59+0430
I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above
I have this string...
2010-08-24T16:00:00-05:00
and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];
Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?
Thanks!
The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?
I think you should be able to do something like the following.
// create the date formatter object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [formatter dateFromString:dateString];
// set up the new date format
[formatter setDateFormat:#"hh:mm:ss"];
NSString *twelveHourTime = [formatter stringFromDate:date];
[formatter release];
Update: Fixed the dateFormatter string format. I had the line below, but the Z seems to be unnecessary. Timezones always screw me up. :-/
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This answer needs to be updated. As of iOS 10 the system provided NSISO8601DateFormatter is available for this particular format.