I am having problems with conversion of NSString object to NSDate. Here is what I want to do:
I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:
"Wed, 25 Sep 2013 12:56:57 GMT"
I tried to convert it to NSDate in this way:
*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*
However, after doing above code, dateNS always appear to be (null).
My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?
By the way, I have seen the website
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
It seems that there are many ways to format particular date, but I could not find what I am doing wrong.
Your problem is the your date formatter has not identical fort as your date string:
You should set date formatter the same format like your date string
My Example:
// Convert string to date
NSString *beginString = #"Sat, 30 Dec 2013 14:45:00 EEST";
//beginString = [beginString stringByReplacingOccurrencesOfString:#"EEST" withString:#""];
//beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Helsinki"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
//NSLog(#"Begin string: %#", beginString);
//NSLog(#"not formated: %#", dateFromString);
// Convert Date to string
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormat setDateFormat:#"dd MMMM yyyy"];
myStrDate = [dateFormat stringFromDate:dateFromString];
[currentTitle setPubDate:myStrDate];
NSDate * dateNS = [[NSDate alloc] init]; is useless, you don't need to allocate any date object.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSLog(#"%#", [dateFormatter dateFromString:#"Wed, 25 Sep 2013 12:56:57 GMT"]);
Outputs the date correctly, are you sure theString isn't nil?
Related
I know that it sounds incessantly familiar, but most of the suggested solutions on SO have not worked for me for some strange reason.
I have a date string returned from an SQLite query as an NSString in this format:
2019-06-10 13:45:33
However, when any of the suggested date formatter solutions are applied, with or without timezone localisation, I keep getting such a result:
Mon Jun 10 13:45:33 2019
This is one of the routines I've tried, among many others:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
// dateFromString > Mon Jun 10 13:45:33 2019
Could I be doing something wrong or is there some missing step in the conversion?
TIA.
I could guess that you wanted another output format, if it is the case then you could try code like this:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDateFormatter *printDateFormatter = [[NSDateFormatter alloc] init];
printDateFormatter.dateStyle = NSDateIntervalFormatterMediumStyle;
printDateFormatter.timeStyle = NSDateIntervalFormatterMediumStyle;
NSLog(#"%#", [printDateFormatter stringFromDate:dateFromString]);
The result will be:
10 Jun 2019 at 13:45:33
Use a different formatter to format the string from the date. For example:
NSDateFormatter * formatter=[[NSDateFormatter]alloc]init];
formatter.dateStyle = NSDateFormatterMediumStyle;
formatter.timeStyle = NSDateFormatterNoStyle;
NSString * formattedDateString = [formatter stringFromDate:date];
setDateFormat is for inputting date strings and getting NSDates.
dateStyle and timeStyle is for formatting dateStrings from NSDates.
I am facing issue while converting date and time in device's current (Local) date and time.
Am getting date in format 2013-04-03 01:07:56 +0000 from API and I want to convert this date in local date and time format and display. Can anyone please suggest me how can I achieve this?
Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];
I got a NSString *string = #"2012-10-24 23:00:00 +0000";
And I want to convert that to a normal format 24 october 2012
I use the following code. But it keeps crashing. Can anyone help me?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy MM dd HH:mm:SS zzz"];
NSDate *dateNS = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MM yyyy"];
date = [dateformat stringFromDate:dateNS];
[dateformat release]
It keeps crashing because it can't create the NSDate from the input date because the dateformat is incorrect. it missing the - between the date and the seconds are ss not SS.
Also you need a date formate with MMMM to get the months full name:
NSString *string = #"2012-10-24 23:00:00 +0000";
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateNS = [dateformat dateFromString:string];
[dateformat setDateFormat:#"dd MMMM yyyy"];
NSString *date = [dateformat stringFromDate:dateNS];
[dateformat release];
I've a webservice which gives back my date in the following way.
Wed Oct 31 11:59:44 +0000 2012
But I want it to give it back in this way
31-10-2012 11:59
I know that it should be done with a NSDateFormatter. But I don't now how to implement it in the correct way.
I've something like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:DATE]];
Can anybody help me?
Kind regards.
Code at the moment
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *date2 = [f2 stringFromDate:date];
Webservice layout
"text": "KRC Genk | Zaterdag is er opnieuw een open stadiontour http://t.co/tSbZ2fYG",
"created_at": "Fri Nov 02 12:49:34 +0000 2012"
Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];
desiredformat = s;
P.S. I'm not sure of f format, check this link
http://www.developers-life.com/nsdateformatter-and-uifont.html
There are a few issues with your format string for parsing the original date. And the locale isn't set properly. There is no need to set the timezone. That will be processed from the supplied date/time string.
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[f setLocale:posix];
[f setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
You want to use the en_US_POSIX locale whenever you are parsing (or formatting) a fixed format date that is not from or for a user. Do not use the en_US_POSIX locale to display dates or times to a user.
I've one problem with NSDate and NSDateFormatter.
From this NSString
NSString *startDate = #"Thu, 19 Apr 2012 13:09:56 +0000";
I would obtain a date localized with current locale like this "Thu, 19 Apr 2012 13:09".
I've elaborate this code but the 'endDate' string return nil (within the debugger obtain 'invalid CFStringRef').
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy HH:mm"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *endDate = [dateFormatter stringFromDate:dateFromString];
Where is the bug?
Alex.
It seems that the code executed in a non-US locale. I use non-US locales. I have the same nil output also.
It seems like we need to setLocale: before setDateFormat:.
Below code works ok:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Ya its working correctly..Try removing autorelease from your dateFormatter and then try it again.
And i hope this link is same as yours
Invalid CFStringRef issue
Try this answer and change your format option with "ZZ" and not "ZZZ".