What is wrong with this code?
NSString *strTest = #"Sun Apr 12 23:29:24 +0000 2009";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy"];
NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
self.createdAt.text = [dateFormatter stringFromDate:createdAtFormatted];
self.createdAt is a UILabel and it stays empty. Why?
When trying to use dateWithNaturalLanguageString, I get:
Your date formatter date format does not match the date format from the strTest string.
NSString *strTest = #"Sun Apr 12 23:29:24 +0000 2009";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]; // so the formatter recognizes english names for months and days
[dateFormatter setLocale:loc];
[loc release]; // remove if using ARC
[dateFormatter setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
NSLog(#"got %#", createdAtFormatted);
The formatter specifiers can be found here.
You are giving MM/dd/yy pattern to dateFormatter but your date string is not fit for that thus dateFormatter returning a nil date.
Related
Input: Fri, 26 Apr 2013 21:56:31 EDT
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"EE, dd MMM YYYY HH:mm:ss zzz"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]];
NSLog(#"currentDate: %#", startDateStr2);
but i am getting output : Sat, 05 Jan 2013 07:26:31 GMT+05:30
Use below method which is used to change the format of the NSDate..
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
Call above method with its 3 parameters..
1. stringDate : Pass your date which in string type.
2. dateFormat : set format which is used in your passed string date. For example, if your string date is "14-03-2013 11:22 am" then set the format to #"dd-MM-yyyy hh:mm a".
3. getwithFormat : Set format which you want, as if you want date 14/03/2013 then just set format #"dd/MM/yyyy".
How to call this see below example with method:
NSString *strSDate = [self changeDateFormat:#"14-03-2013 11:22 am" dateFormat:#"dd-MM-yyyy hh:mm a" getwithFormat:#"dd/MM/yyyy"];
NSLog(#"\n\n Now Date => %#",strSDate);
Output is : Now Date => 14/03/2013
hope its helpful to you...
I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])
Output format: date and day format is in the form of 05 March 2012, MondayNSDate *selected = [picker date];
In this code I got output in the form of 1989-07-03, I need to print the corresponding day.
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy-MM-dd"];
// [formatter setDateFormat:#"dd %b YYYY, %a"];
//Get the string date
NSString* date = [formatter stringFromDate:selected];
//Display on the console
NSLog(#"%#",date);
Use this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd %b YYYY, %a"];
NSString *theString = [formatter stringFromDate:theDate];
%b is the name of the month, %a is the name of the day
More info here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
dd MM yyyy,EEE like this ?
you can see more info about dateformatter Date_Format_Patterns
use this
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy,EEEE"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];
Instead of using a fixed format, you should chose a style which will be localized:
[format setDateStyle:NSDateFormatterFullStyle];
This way the user will see the date in the date format that he's used to without you needing to worry about it. For example, Americans would get dates like "Tuesday, April 12, 1952 AD" while Germans would get
"Dienstag, 12. April 1952".
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];
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);
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.