Date formatting in iOS 9 not working [duplicate] - objective-c

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")

Related

Why is an NSString date value being converted to GMT by NSDateFormatter? [duplicate]

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.

How to init a NSDate with a NSString? [duplicate]

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

NSDateFormatter dateFromString produces nil after first call in same method

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.

dateFromString works different in iOS 4.3.4 ?

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.

Objective-C and sqlite's DATETIME type

I have a sqlite3 table that I'm trying to map to an object in objective-C. One attribute of the table is 'completed_at' which is stored as a DATETIME.
I want to create a property on my objective-C class (which inherits from NSObject) that will map well to the 'completed_at' attribute.
Objective-C has an NSDate type but I'm not sure if that will map directly?
I am sharing here just the core things regarding date formatting for saving and retrieving the data for presentation. If you have any problem with this code snippet then I will share the full code that I used for my project.
When you save your data, bind your date value in the sql statement like this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString=[dateFormat stringFromDate:[NSDate date]];
sqlite3_bind_text(saveStmt, 1, [dateString UTF8String] , -1, SQLITE_TRANSIENT);
and when you retrieve data you have to write this code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate =[dateFormat dateFromString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]];
now you have a variable myDate of NSDate type which you can render in your way:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSLog(#"My Date was : %#", [formatter stringFromDate:myDate]);
You must have to remember three things:
In your SQLite date field type should be DATETIME
Date format should be same when you store and when you retrieve
Now you can show in your own way but following the format. Below the format details is given.
Format:
'dd' = Day 01-31
'MM' = Month 01-12
'yyyy' = Year 2000
'HH' = Hour in 24 hour
'hh' = Hour in 12 hour
'mm' = Minute 00-59
'ss' = Second 00-59
'a' = AM / PM
I have zero experience with Objective-C, but I found Apple's NSDate Class Reference with a google search. With the information provided on the linked page you should be able to figure out how to manipulate 32-bit epoch times in Objective-C, and this would work well in SQLite. I would probably create the completed_at column as type INTEGER for 32-bit times.
SQLite really prefers Julian dates, which are floats. I haven't found any documentation explaining how one might coerce the NSDate class into working with Julians.
timeIntervalSince1970 looks very interesting.
This came up a couple of weeks ago:
Persisting Dates to SQLite3 in an iPhone Application
The formatter is important if you are trying to effect the presentation but if you use if for internal storage, you are defining a string which can defeat the DB-engine's ability to use the value for computation, comparison, sorting, etc. Also, if you are going to have different clients inserting the date value into the DB you would have to write conversion functions everywhere. I used the following and it worked as expected (schema's column defined as DATETIME):
dateExpires = [NSDate dateWithTimeIntervalSinceNow: sqlite3_column_double(queryStmt, 5)];
I inserted into the SQLITE3 db with the Firefox add-on as "4/12/2010" here in Central time zone. Viewing the value of 'dateExpires' in XCode-debugger displayed as:
2010-04-12 23:19:48 -0500
Sure enough, that is the correct time.
Also, to insert into the SQLITE DB you will put the value [NSDate date]