Incorrectly formatting an NSDate from string - iphone

I would like to create an NSDate from a string:
NSString *mostRecentMentionMessageTimestamp = [mostRecentMessage valueForKey:#"updated_at"];
NSLog(#"%#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"%#",mostRecentMentionDate);
When I print my string it is:
2011-11-10T07:22:59Z
After I make a date from string and print it, it is (null)
What am I doing wrong?

You need to set the time zone and drop the Z at the end of the date format. Since your date has a Z, it is in UTC. So you can use:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss"];

The format is almost correct, use:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
to solve the time zone issue use the solution by #Jef add:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
Full example code:
NSString *mostRecentMentionMessageTimestamp = #"2011-11-10T07:22:59Z";
NSLog(#"input: %#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"output: %#",mostRecentMentionDate);
NSLog output:
input: 2011-11-10T07:22:59Z
output: 2011-11-10 07:22:59 +0000

the string value which you are trying to convert to NSDate is incorrect.
"2011-11-10T07:22:59Z"
it should have a timezone difference value in place of "Z" to make it compatible to date formatter's format.
e.g. 2011-11-10T07:22:59+0430

Related

Convert NSString to NSDate returns (null) value

I have one Date in string format "2013-03-19T19:00:50"
I am trying to convert it into NSDate using NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);
but it is giving me null date
date in date format : (null)
What is the issue?
Use :
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
You have time in 24 hour format, so HH is required. hh is used when time is in 12 hour format.
And T is required to be in single quote, T is not a part of date this is an added text on it.
According to the date formatter patterns hh means Hour [1-12] You want Hour [0-23] which is HH.
And any letters that are not date format patterns, or must not be interpreted in this way have to be put in between apostrophes.
use [dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Do like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);

Converting NSString to nsdate not working

I am converting date from NSString to NSDate using following code
NSString *dateString = #"julho-29-2012 05:01 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSDate* dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#",dateFromString);
[dateFormatter release];
But it not working for me :(
That's because the formats don't match.
"julho-29-2012 05:01 PM"
Follows the following format:
"MMMM-dd-yyyy hh:mm a"
Basically your date formatter isn't able to understand your string and gives a default value, nil.
Edit
You might need to change the formatters locale since your date strings are not in English but Portuguese.
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"pt-PT"]];
Your date format string doesn't match the date format in the string.
Try:
[dateFormatter setDateFormat:#"MMMM-dd-yyyy hh:mm a"];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"pt"]];
Setting the locale should only be needed if your phone is not in Portuguese already.
your date formatter's format does not match ur date in string format.

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.

iPhone : How to convert date?

My date is coming in string format from server
Output of date come like this "07/30/2011",
I want a date format "30 july"
how can i do that?
Easy, use NSDateFormatter
Example,
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"07/30/2011" ];
[dateFormatter setDateFormat:#"dd MMM"];
NSString *output = [dateFormatter stringFromDate:date];

is there any easy way to get Date?

I'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...
I think there might be a method in NSCalender? If there's such a method, please tell me.
Thanks in advance.
I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls:
NSDate * myDate = [NSDate date];
If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.
I have some code posted here:
How get a datetime column in SQLite with Objective C
that shows how to create NSDates from NSStrings using NSDateFormatter.
If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.
For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:#"2011-01-26 00:00"];
// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(#"date: %#", midnight_26_jan_2011_utc);
[formatter release];
Edit: Added time to format string.
You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:
NSString* defaultDateString = [NSString stringWithFormat:#"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];
and if you wanted to get the string from a date you can just use:
NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need
NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];
Now, you can use the string the date. :)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:#"2011-01-26"];