Hi Im trying to convert an NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:lastModified];
But when I do a
NSLog(#" date: %# ", dateFromString);
I get date: (null)
The lastModified string has date in string format as "Tue, 15 Feb 2011 13:30:42 GMT"
What am I doing wrong?
your input (lastModified) does not match the specified date format.
a properly formed string representation of the time specified by lastModified for the date format is:
"2011-02-15 13:30:42 GMT", but you're passing "Tue, 15 Feb 2011 13:30:42 GMT".
Related
Suppose Twitter returns Wed Sep 14 18:52:57 +0000 2011in JSON format. How do i go about parsing it so that the display looks something like Sep 2011.
Im aware of DateFormatter. I tried the following code but it keeps returning me Null.
NSString *created = [(NSDictionary *)TWData objectForKey:#"created_at"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM yyyy"];
NSDate *date = [dateFormatter dateFromString:created];
tweetingSince.text= [NSString stringWithFormat:#"#%#",[dateFormatter stringFromDate:date]];
You are using same date format to parse the input string created to NSDate and to create final output format from the parsed date. Date formatter is unable to parse date Wed Sep 14 18:52:57 +0000 2011 using date format MMM yyyy. This is why the date is nil.
Edit:
Also quick googling gave me this result on SO: iPhone + Twitter API: Converting time?
Edit 2: Your code with proper NSDate parsing would look like this
NSString *created = [(NSDictionary *)TWData objectForKey:#"created_at"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
// Parse input string to NSDate
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:created];
// Convert parsed date to output format
[dateFormatter setDateFormat:#"MMM yyyy"];
tweetingSince.text= [NSString stringWithFormat:#"#%#",[dateFormatter stringFromDate:date]];
You can do it like this
NSString *myString = #"Wed Sep 14 18:52:57 +0000 2011";
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:#" "];
NSArray *components = [myString componentsSeparatedByCharactersInSet:delimiters];
NSLog(#"dilip-%#",components);
Output will be
dilip-(
Wed,
Sep,
14,
"18:52:57",
"+0000",
2011
)
Now you can select any value from array and create new string using that value.
NSString * firstStr = [components objectAtIndex:1];
NSString * secondStr = [components objectAtIndex:5];
NSString * fullStr = [NSString stringWithFormat:#"%# %#",firstStr,secondStr];
NSLog(#"dilip - %#",fullStr);
Output will be
dilip - Sep 2011
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...
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 the date that is in format 2010-11-17.This is of the form NSString.
I want to convert this NSString date into format 17 Nov 2010 and display in a label
This date is just not the current date but may even be the older dates.
So I cant use the [NSDate date] instance to get the date.
I tried using NSDateFormatter with the method dateFromString.
But it gives me a null value.
Here is the code snippet
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);
What should be done?
Please Help and suggest.
Thanks.
Try this
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"yyyy-MM-dd"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"d MMM yyyy"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"RESULT %#",result);
I got the result.Correct me if this approach is wrong.
Here are some formats for who desire to know!!
12:16:45 PM on January 01, 2000 --> hh:mm:ss a 'on' MMMM dd, yyyy
Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy--
09/12/2018 --> MM/dd/yyyy--
09-12-2018 14:11 --> MM-dd-yyyy HH:mm--
Sep 12, 2:11 PM --> MMM d, h:mm a--
September 2018 --> MMMM yyyy--
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
12:16:45 PM --> hh:mm:ss a
AM or PM --> a
NSDateFormatter uses date format patterns from UNICODE.
So for your 17 Nov 2010 date you need something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d MMM yyyy"];
NSString *result = [formatter stringForObjectValue:date];
Where date variable contains the date you'd like to format.
UPDATE: Try changing your code to this:
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);
I need the date as a string but not the time and it has to be localized.
So for example USA should be Sep 25 2009 but for New Zealand it would be 25 Sep 2009.
I can get the date into a string by specifying the format "MMM dd YYYY" but It's not localized.
Any ideas?
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:0]
The key is to send setTimeStyle:kCFDateFormatterNoStyle to the dateFormatter. That sets the dateFormatter so that it will only format the date and not output the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
This gives me the following output:
12/18/09