I want to convert this ,
NSString *result1=#"Mon, 28 Sep 2009 06:35:42 PDT";
to nsdate using NSDateFormatter in iphone....
Can anyone help me?
Thanks in advance.....
I believe you want this:
NSString* dateString = #"Mon, 28 Sep 2009 06:35:42 PDT";
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
// Use this format to parse the string
[newFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
// Now change the format to that desired for printing
[newFormatter setDateFormat:#"MMM dd,yyyy HH:mm:ss aaa"];
NSLog(#"%#", [newFormatter stringFromDate:aDate]);
// Result:
// 2009-09-29 23:50:09.440 test[15396:903] Sep 28,2009 06:35:42 AM
You can find these codes here (as referenced in the NSDateFormatter documentation): http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
That's what I do in my program:
NSString *dateString = #"Mon, 28 Sep 2009 06:35:42 PDT";
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateStyle:NSDateFormatterMediumStyle];
[newFormatter setTimeStyle:NSDateFormatterMediumStyle];
I'm using medium style, but there are more styles, you probably should use kCFDateFormatterLongStyle or kCFDateFormatterFullStyle, and then:
NSDate *aDate = [newFormatter dateFromString: dateString];
[newFormatter release];
Hope this helps
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 am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....
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.
I have a NSString which is passed from an xml feed........
NSString *strDate =#"Thu, 22 Apr 2010 10.30 am CEST";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a vvvv"];
NSDate *myDate = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:#"HH"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display only hours and currently I'm getting value like this.
strDate =#"2010-04-10 14:00:00 +0530";
Can anyone please help me with this?......
I'm sorry.It's my mistake.It should be like this.
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
What my requirement is to get hour part only from above string using NSDateFormatter. How can I achieve that. Sorry for the earlier mistake.
Thanks.
If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like:
strDate = #"Thu, 22 Apr 2010 10:30 am CEST";
NSArray *dateComponents = [strDate componentsSeparatedByString:#" "];
NSString *requiredString = [dateComponents objectAtIndex:4];
dateComponents = [requiredString componentsSeparatedByString:#":"];
requiredString = [dateComponents objectAtIndex:0];
and when you do:
NSLog(rquiredString);
Output : 10;
This is just a workaround, for better approach you should go through the NSDateComponents class.
Hope this helps.
Thanks,
Madhup
change to
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh.mm a vvvv"];
(. instead of : in hh:mm)
You want to do this:
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"hh"];
strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
(Firstly your original formatter was wrong, you had a: instead of a .) EDIT no longer the case
Secondly, you want to ignore the CEST bit as this will cause your timezone being changed
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.