Is there any changes regarding NSDateFormatter in iOS 4.3.4? Because i upgraded my iTouch os to 4.3.4 yesterday and found that some of my code related to NSDateFormatter is stop to work.
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
NSDate *date_ = [outputFormatter dateFromString:#"08:00 AM"];
NSLog(#"Date:%#",date_);
[outputFormatter release];
OUTPUT
iOS 4.3.1 : 1970-01-01 02:30:00 +0000
iOS 4.3.4 : null.
Any ideas????
First of all, your code does NOT work in iOS 4.3.1... If you look at the date, you should notice that it is January 1970, or the common "year zero" in computer programming.
I think you should have put "HH:mm" instead of "h:mm a". The doubled h doesn't mean two digits, it is just some convention made up by ISO. And regarding the "AM-PM" parameter, I think it comes from locale.
Look at the documentation of NSDate for setLocale. Your parameter should be #"en-US" I think.
I hope it helped you.
Regards.
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")
Before Xcode 4 I used to use NSDate initFromString but it is now deprecated and produces errors in Xcode 4.2. So I jumped over to using NSDateFormatter dateFromString but ran into an issue in a method I call that gets a sunrise date from string and a sunset date from string to determine if it is day or night (so same method). The first call works fine, but the second call returns nil. I decided to see if it was repeatable so I created the following simple method and dumped it into another project in an innocuous place:
- (void)testDateFormatter
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"yyyy-MM-dd hh:mm:ss Z"];
NSString *srDateTimeString = #"2011-11-09 07:08:00 -0800";
NSString *ssDateTimeString = #"2011-11-09 17:08:00 -0800";
NSDate *sunriseDateTime = [formatter dateFromString: srDateTimeString];
NSDate *sunsetDateTime = [formatter dateFromString: ssDateTimeString];
return;
}
The first call returns the correct date. The second call returns nil. I have tried several variations (such as creating two separate NSDateFormatters, preinitializing the dates, preinitializing the dates to the current time and then reassigning) but none do the expected thing.
Does anyone know what is going on here? Since I hadn't found any reference to this error anywhere I submitted it to Apple (bug #10420498).
Jack
In the second string, the hour is 17 (24-hour format) but the format string uses hh (lowercase) which is for 12-hour format.
Change the format string to yyyy-MM-dd HH:mm:ss Z.
In the documentation, see Date Formatters which contains a link to the Unicode Date Format Patterns listing each format specifier.
I run the following code:
NSDate *now = [NSDate date];
NSLog(#"now: %#", now);
and get :
2011-09-16 16:14:16.434 iSavemore[1229:7907] now: 2011-09-16 21:14:16 +0000
As you can see i'm running this at 16:14:16 (4:14 pm) but NSDate is returning 21:16:16 (9:14 pm!). Is this an Xcode4 issue or NSDate issue?
NSDate defaults to the Universal timezone (aka GMT).
I'm guessing you're somewhere on the East Coast, 5 hours behind UTC.
Try adding this to your date formatter...
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
...and you should see your local time.
If you want to use a specified locale, rather than 'currentLocale', create a NSLocale for the relevant locale.
NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLoc];
...actually that's US (so possibly not Central).
More specific timezone help can be found here...
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
However, if you want to show expiry time, wouldn't you still want it in the user's currentLocale?
If you look at the output you'll see that the log includes the timezone:
2011-09-16 16:14:16.434 iSavemore[1229:7907] now: 2011-09-16 21:14:16 +0000
^^^^^^
The time stamp of your log is local time. I assume you're in a timezone that is 5 hours ahead of UTC.
A NSDate refers to a particular point in time. It's up to you to display this however you want; usually with an NSDateFormatter.
This is the reason why you'll see plenty of recommendations against storing a time, or a date as anything other than an NSDate. If you try and store it as a string you'll run into a lot of trouble later on when trying to handle the display in different timezones.
Try setting the time-zone of your NSDate to one that is fitting your need, for example [NSTimeZone localTimeZone]
Just a wild guess here, but maybe it has something to do with time zones?
I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ which outputs 2011-04-05 16:28:22 -07:00. How do I parse that in iOS?
This is the code I'm messing around with right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:#"2011-04-05T16:28:22-0700"];
NSLog(#"%#; %#; %#", dateFormatter, date, [NSTimeZone localTimeZone]);
First thing to note is that 2011-04-05 16:28:22 -07:00 has to look like 2011-04-05T16:28:22-0700, where a T replaces the first space (assuming that stands for time, or where the time part of the string starts from?), the second space is removed and the colon in the time zone is removed. I figure I'll find a way to format the string that .NET is sending back to conform to the string iOS will parse.
The real issue is that the date that is outputted is 7 hours ahead of what I've sent in the JSON response. So, iOS outputs 2011-04-05 16:28:22 -07:00 as 2011-04-05 23:28:22 +0000, and that is wrong as far as my app is concerned.
The only solution I've found so far is to send the date in the JSON as 2011-04-05 16:28:22 +00:00, but that again is wrong because I'm altering what the real date should be.
Anyway, I'd appreciate someone taking a look and letting me know how I can parse the date string .NET is outputting via the format yyyy-MM-dd HH:mm:ss ZZZ (which I suppose can be re-written to yyyy-MM-ddTHH:mm:ssZZZ) to an NSDate object I can use in iOS.
I don't know how right this is, but I ultimately found that in .NET I have to do DateTime.ToUniversalTime().ToString("yyyy-MM-ddHH:mm:ss") and on the iOS side I have to do this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"2011-04-0600:28:27"];
Only then is the date correct when NSLog outputs, so I'm assuming that I've finally got a proper date/time.
Your iOS date parsing code is correct: 2011-04-05 16:28:22 -07:00 and 2011-04-05 23:28:22 +0000 represent the same time, just in different time zones. Your only problem is that NSDate doesn't actually store the time zone, and so [date description] outputs using UTC.
You're almost certainly better off just using UTC time everywhere for interchange and not bothering with time zones. I'm sure you can get that your ASP.NET code to do that, and it's easier to parse on the receiving end as well. NSDateFormatter uses standard Unicode date formatting syntax, which you can read about here.
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:].