I am having an issue with this message. It doesn't seem to be able to convert to NSDate with my dateString
//dateString = #"2012-03-24 00:00:00 +0000"
+ (NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *result = [dateFormatter dateFromString:dateString]; //Invalid CFStringRef
[dateFormatter release];
return result;
}
If the time zone is specified with an offset +0000 in
NSString *dateString = #"2012-03-24 00:00:00 +0000";
you have to use:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZ"];
Notice the double 'Z' and the removal of 'T'.
Check here for more information.
Your date string doesn't have a Z on the end (which the format expects) and it does have a time zone offset (which the format does not expect).
Related
I have an NSString like this :
NSString *playTime = #"20:45";// GMT
I need to convert this string to NSDate (only hours and seconds) then adjusted to device timezone.
Example:
If user device timezone is set to GMT, I need a result like this : #"20:45";
If user device timezone is set to GMT + 2, I need a result like this : #"22:45";
Try this
NSString *gmtDateString = #"20:45";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"HH:mm"];
//Now you have date in GMT time zone by default dateFormatter timezone would be in local time zone
NSDate *date = [dateFormatter dateFromString:dateString];
//set to local time zone
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//Now you have string in localDate format
NSString *localDateString = [dateFormatter stringFromDate:date];
The below code will calculate the time w.r.t. local time zone.
Suppose my time zone is GMT+05:30 so the time 05:30 will become 00:00.
NSString *playTime = #"20:45";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"HH:mm";
NSDate *date = [formatter dateFromString:playTime];
NSLog(#"%#",date);
Log is: 2000-01-01 15:15:00 +0000
Use this code for date conversion as per your requirement.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"HH:mm"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
logDate = [dateFormat dateFromString:#"20:45"];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);
I am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....
I can't convert my NSString to NSDate. here's the code:
+ (NSDate *) stringToNSDate: (NSString *) dateString{
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateString: %# datefromstring: %#", dateString, dateFromString);
return dateFromString;
}
//dateFromString is nil.
What should I do? Thanks!
btw, dateString will always contain a string-ed date in this format: "2012/02/10 08:01:25 +0000"
From the information you've given, the problem appears to be that your dateString separates the date components with slashes (\), but your date formatter is expecting dashes (-).
Edit
To answer your other question, you can use stringByReplacingOccurrencesOfString:withString:.
For example:
NSString *date1 = #"2012/12/10 ...";
NSString *date2 = [date1 stringByReplacingOccurrencesOfString:#"/" withString:#"-"];
Note that this will replace every / with -, so just be careful about timezones etc.
Try this instead:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm:ss ZZZ"];
I know this been asked for so many times but I always end up getting null in my NSDate. I have a string like this "January 16, 2012 21:44:56" I want to convert it to "January 16, 2012 09:44:56 PM". I want to add a PM in the converted date and convert the 24 hour time format to 12 hour time format. Here's my code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:ii:ss a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
As Ali3n correctly pointed out, you should first set the format of dateString to the formatter to get a valid date object. Next you should set the formatter's format to the desired one and continue. Do the following:
NSString *dateString = #"January 16, 2012 21:44:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:mm:ss a"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
#"MMMM dd, YYYY HH:ii:ss a" this format should match with the date the ypu are passing to the date formatter ..
There is an error in your format string an also you need to tell the formatter the Locale in which your date string is presented.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss a"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release], dateFormatter = nil;
Setting the Local is very important since you have an name of a date in your input. You will need to tell the NSDateFormatter is wich language the name will be. In the example given it is in english. I've you run you code without setting the local on a device where the language is not set to english it wil fail to parse the date.
Try to escape literals in the format string.
[dateFormatter setDateFormat:#"MMMM dd',' YYYY HH:ii:ss a"];
As for your requirements you have to change the dateFormatter.Check this link for more.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSLog(#"%#",dateString);
I have the following date:
2011-10-20T01:10:50Z
I would like it to be formatted to
"M/d/yy 'at' h:mma"
Here is my code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-dd-MM'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:#"created_at"]];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
[dateFormatter release];
NSLog(#"%#", finalTime);
Unfortunately the finalTime is NULL here.
You have dd-MM backwards. You have 10 for dd and 20 MM. There is no month 20.
2011-10-20T01:10:50Z
#"yyyy-dd-MM'T'HH:mm:ss'Z'"
Because of that, the newTime was null and the finalTime was null.
Here's with the fix. This:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:#"2011-10-20T01:10:50Z"];
NSLog(#"original time: %#", newTime);
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);
[dateFormatter release];
Outputs:
2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM
This technical note helped me resolve the same issue: https://developer.apple.com/library/ios/qa/qa1480/_index.html
In a nutshell, you need to set 3 things on NSDateFormatter:
Locale
TimeZone
DateFormat
code sample:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2014-10-23T01:10:50.000Z"];
I was using NSDateFormatter to successfully parse UTC strings to NSDate by only setting time zone and date format. When it suddenly stopped working I found this tech note and added setLocal: which resolved the issue for me.
Because Swift:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let dateHopefully = dateFormatter.dateFromString("2016-07-29T17:15:02Z")
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
dateFormatter.dateFormat = "M/d/yy 'at' h:mma"
let lastTime = dateFormatter.stringFromDate(dateHopefully!)
print("Here is the hopeful date \(dateHopefully) and here is the lastTime \(lastTime)")