NSDate from NSString problems - iphone

Getting a NSString as follows:
NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:#"LastSyncDate"];
This value is: 1/1/2010 12:00:00 AM
I am using the following to convert to a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:#"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];
This gives me a null value?

Your date format doesn't match the format of the string.
Try
[df setDateFormat:#"M/d/y hh:mm:ss a"];
or
[df setDateFormat:#"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded
The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.

Related

NSDateFormatter formatting issue

I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.

NSDate from NSString in iPhone

One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.

How to make date conversion?

I am getting date in string formate ad "2012-04-11T07:51:10+0000" & I want to convert it to NSDate as "11-04-2012". I dont understand how to use "setDateFormat" for this.
This is what I am doing--
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'zzzz"];
Please help.
And whatever NSDate I will get I again want to convert to string ie. only 11-04-2012.
I have searched on ggl. But no satisfactory result :(
please help.
Here is how to parse the string to generate a date:
NSString *dateString = #"2012-04-11T07:51:10+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyy-MM-dd'T'HH:mm:ssZ";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
Now you can generate a new string representation of the date as follows:
formatter.dateFormat = #"dd-MM-yyyy";
NSString *newDateString = [formatter stringFromDate:date];
NSLog(#"New string: %#", newDateString);
// 11-04-2012
The problem with the dateFormat you were using is that you don't have to escape all the symbols such as 'MM' and that zzzz is not the correct symbol to parse the timezone of type +0000.
Have a look at this documentation for more information.
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100"; // change value base on your requirement.
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"]; // Change format base on dareStr.
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"]; // Change format base on your output requirement
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];

NSString to NSDate keeps getting null

I know this been asked for so many times but I always end up getting null in my NSDate. I have a string like this "January 16, 2012 21:44:56" I want to convert it to "January 16, 2012 09:44:56 PM". I want to add a PM in the converted date and convert the 24 hour time format to 12 hour time format. Here's my code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:ii:ss a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
As Ali3n correctly pointed out, you should first set the format of dateString to the formatter to get a valid date object. Next you should set the formatter's format to the desired one and continue. Do the following:
NSString *dateString = #"January 16, 2012 21:44:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:mm:ss a"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
#"MMMM dd, YYYY HH:ii:ss a" this format should match with the date the ypu are passing to the date formatter ..
There is an error in your format string an also you need to tell the formatter the Locale in which your date string is presented.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss a"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release], dateFormatter = nil;
Setting the Local is very important since you have an name of a date in your input. You will need to tell the NSDateFormatter is wich language the name will be. In the example given it is in english. I've you run you code without setting the local on a device where the language is not set to english it wil fail to parse the date.
Try to escape literals in the format string.
[dateFormatter setDateFormat:#"MMMM dd',' YYYY HH:ii:ss a"];
As for your requirements you have to change the dateFormatter.Check this link for more.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSLog(#"%#",dateString);

How I can set a date string from twitter to a NSDATE

The date you get back from twitter is in this format Fri Aug 07 12:40:04 +0000 2009. I am able to assign the value to a NSDate without issue. However, when I attempt to use NSDateFormatter, I get a nil returned to me. What am I missing?
NSDate *createdAt = [messageData objectForKey:#"created_at"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"M/d/yy HH:mm"];
NSString *dateString = [format stringFromDate:createdAt];
label.text = dateString;
i had the same question, and i could not resolve it with the current above answers. so here is what worked for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
[df setDateFormat:#"eee MMM dd yyyy"];
NSString *dateStr = [df stringFromDate:date];
where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date
If the object associated with the #"created_at" key is a valid NSDate object, this code should work.
However, I'm guessing that it is actually an NSString. If so, it will produce the behavior you're describing.
If I'm right, the code snippet above is assigning an NSString object to an NSDate reference. NSDictionary returns untyped 'id' objects, so the compiler won't give you a type mismatch warning.
You'll have to use NSDateFormatter to parse the string into an NSDate (see dateFromString:).
It looks likes, Twitter changes the format again and again. These works for me now. Don't forget to set the locale.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Mon, 29 Oct 2012 12:24:50 +0000
[df setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss ZZZ"];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate *date = [df dateFromString:[element valueForKey:#"created_at"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateStr = [df stringFromDate:date];
[element valueForKey:#"created_at"] - date from twitter
Above nothing worked for me - try this for Twitter: (I've also included a nice nsdate category for better showing date
lbl = (UILabel *)[cell viewWithTag:16];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//"created_at": "Tue Jul 09 07:45:01 +0000 2013",
[df setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSTimeInterval timeIntveral = [[df dateFromString:[[_twitterArray objectAtIndex:indexPath.row] objectForKey:#"created_at"]] timeIntervalSince1970];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:timeIntveral];
//will work if you added the nsdate category, linked above
NSString *ago = [date timeAgo];
NSLog(#"Output is: \"%#\" %.f", ago,timeIntveral);
lbl.text = ago;
Hope this helps!
First off, what are you using stringFromDate: for? That's if you already have an NSDate and want to make a string representing it.
Moreover, when you do use the date formatter, you're giving it a format string that doesn't match the date string you're trying to interpret.
Change the format string to match your date strings, and use dateFromString: instead of stringFromDate: (with the attendant changes to your variable declarations), and it should work.