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)
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")
I have looked at a number of other solutions on here, but can't seem to get this to work for my case.
I am consuming an API, from which some parts will be saved to core data, however the date format in the API doesn't match that of which core data expects,
The date that I am getting is in the format:
Jun 28, 2013 5:51:28 PM
I need to be able to sort my entity by this date in order to display the latest items. I tried the following format, but I can't seem to get a result that works, any ideas on how I can achieve this?
NSDate *createdDate = [key objectForKey:#"createdDate"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"mm/dd/yyyy hhmmss"];
NSDate *date = [dateFormat dateFromString:createdDate];
createdDate can't be an NSDate. If it was your code would crash rather than just not working. It must be an NSString.
NSString *createdDate = [key objectForKey:#"createdDate"];
Next, the date string you say you receive is Jun 28, 2013 5:51:28 PM which doesn't even slightly match the format you're trying to use of mm/dd/yyyy hhmmss. For a start the format has slashes and the date string doesn't. The format must match the string construction exactly. Read the date formatter format spec again and modify your format. I don't have it to hand but it will be something like MMM dd, yyyy H:mm:ss a (don't just use that format, check it first).
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.
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
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]