This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I'm still beginner at obj-C/iOS development... I've created a class containing a NSDate *date, and I get the data from a XML file. So I want to do something like this :
[myItem setDate:[NSDate dateWithString:xmlDate]];
I get the error :
No known class method for selector 'dateWithString:'
What is the best way to do it ? Thanks
You need an NSDateFormatter. See this previous question or this previous question or this previous question or just the Apple Date Formatter guide for more details.
And please remember to search for duplicate questions before posting.
I would recommend you to use a NSDateFormatter instance for this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// set the appropriate format in this string
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:xmlDate];
[dateFormatter release];
To make an NSDate object from a NSString you should use NSDateFormatter. More info can be found on Apple's website:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Hope it helps
Related
This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 7 years ago.
I'm have a problem with date formatting in iOS 9, it works ok with iOS 8 and also it works when I'm testing it in simulator with iOS 9, but when a test it on real device with iOS 9 I'm getting null. Below is code that I'm using
NSLog(#"String Date: '%#'", stringDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:stringDate];
NSLog(#"Date: %#", date);
In log I'm getting:
String Date: '8/29/2015 4:13:39 PM'
Date: (null)
Also if I use uppercase h (H or HH) I'm always getting that hour is 10.
Try setting locale on your date formatter.
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
From Apple Documentation:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
EDIT: Swift Version
dateFormat.locale = NSLocale(localeIdentifier: "en_US_POSIX")
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm pretty new to objective-c and I have a date stored in a sqlite table as an integer e.g. 20131017 (which represents Oct 17, 2013). I want to display this to the user in the format Oct 17, 2013 but my code produces a null when I log it.
No doubt something very fundamental I'm missing - but for the life of me I can't see it.
Here's the code:
_fromDate.text = _startDate; //e.g.20131017 (representing Oct 17, 2013)
NSString *dateString =[NSString stringWithFormat:#"%d", [_fromDate.text intValue]];
NSLog(#"dateString: %#", dateString);// shows 20131017 ok
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd, yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"date: %#", date); //produces a nil result
Appreciate any help. Thx.
With this bit of code:
[dateFormat setDateFormat:#"MMM dd, yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
you're saying:
This is my date format
Convert this string, which is in the format I just told you about, into a date
But the string isn't in that format, so it doesn't work.
The format is actually: #"yyyyMMdd" (or something like that, I don't have the spec to hand).
You really need 2 formats (and 2 formatters)...
The format to convert your stored string to a date
The format to convert your date into your display string (this is the one you already had)
When I format a date via:
[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]];
save it somewhere, then try to access it, i get a NSString. How to get it as a NSDate?
BTW: I want a method where i put NSDateFormatterStyle as argument. Otherwise it will be wrong- in different locale it will be saved as different string, so formatting it as
[dateFormat setDateFormat:#"yy/MM/dd"];
or any other options of this kind - will create an error. Or at least i think so ;).
Thanks for any responses.
NSDateFormatter returns a NSString representation of the NSDate object. NSDate is format insensitive, meaning it isn't tied to some locale, it's actually stored as a number. You use the NSDateFormatter to present that numerical value as a localized representation.
To convert a NSString back to an NSDate you can use something like this:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yy/MM/dd"];
NSDate *myDate = [df dateFromString: dateString];
It sounds like you want to store the NSDate in a way that can be perfectly parsed both ways. In that case I'd recommend storing it as an ISO8601 date, and you'll need to use an nsdateformatter more custom-like to do that. There are plenty of libraries/categories out there on github or stackoverflow to help you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSDate / NSDateFormatter returning GMT on iPhone iOS 4.3
When the following code is executed
NSString *inputDateString=#"2012-10-19 19:37:54";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *test=[formatter dateFromString:inputDateString];
the value of "test" is 2012-10-19 23:37:54 +0000 rather than a date value equivalent to the inputDateString. Why is the date being converted to GMT?
EDIT: How do I take an input string date and and convert it to an equivalent NSDate object?
NSDate doesn't store timezones, nor does it care about them. NSDate only stores the time that has passed since a given reference date (january 1st 2001). When you use your NSDateFormatter like this, it will assume that the timezone of the date matches the users current timezone when converting the string to the date object, however, when you use NSLog to check the date, it doesn't have any timezone information. Keep in mind though, that the date is still correct and equal to the input value.
I'm fighting with a strange situation: same code works different in two different projects. The one project is just empty command line utility with this code. The second project is with linked gdata-objectivec-client library.
Here is the code:
static NSString * const dateFormat = #"MM/dd/yyyy HH:mm:ss Z";
NSString *tmp_string = #"03/08/2011 10:07:36 +0300";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease] ;
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat: dateFormat ];
NSDate *newDate = [dateFormatter dateFromString: tmp_string];
NSLog(#"dateFromThatString: %#", newDate);
In just command line utility the result is same
"03/08/2011 10:07:36 +0300"
.
But in the project with gdata-objectivec-client linked to it, the result is changed to
"03/08/2011 07:07:36 +0000"
I cant find what's the problem, any suggestions?
Reading about this subject i've learned that "NSDate is not aware of time zones, it always stores dates in a time zone independent manner (as a span of time since a specific reference date)", so those two NSDate objects representing two different strings in two different projects are the same, there is just some problem in difference between description of NSDate objects, so.. it's not a big problem for future work, because i needed these description only for an easy debug. I will just not use description method, but [NSFormatted stringFromDate:].
It's interesting how gdata-objectivec-client influenced on a project, that description of nsdate obj returns same time, but responding to +0000 gmt offset.
But it's only for discussion.
It looks like the date formatter has different time zones in each case. You can change the time zone using -[NSDateFormatter setTimeZone:].