I am having a trouble converting NSString to NSDate.
I have a date string like 'Mon Dec 13 14:55:05 +0800 2010', here is my code:
NSString *dateString = [NSString stringWithFormat:#"Mon Dec 13 14:55:05 +0800 2010"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd hh:mm:ss zzz yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
Odd, It works fine yesterday, but I'm getting nil now.
Any suggestions would be greatly appreciated. Thanks.
24-hour notation needs an HH string representation instead of hh so you should have
#"EEE MMM dd HH:mm:ss zzz yyyy"
Related
Here is my code :
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:[line substringToIndex:24]];
line consist of string : "Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :"
I get the date alone "Mon Feb 18 09:25:53 2013" and formatted it with #"EEE MM d HH:mm:ss yyyy" format.
I get an incorrect output : "2013-02-18 03:55:53 -0000" Time alone is printed incorrectly. I followed Date Format Patterns for specifying patters but still I am phasing this issue. I am not able to understand where it going wrong.. It would be helpful if someone finds the problem.
thanks in advance
Try this one
NSDate *date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"EEE MM d hh:mm:ss yyyy"];
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
When you just log using NSLog you get the UTC format (UTC consists of offset value, in India its standard time +5.30)that is yyyy-MM-d HH:mm:ss z , z is the offset added or subtracted to or from current time based on the locality.
For example
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
Tue 02 26 05:38:00 2013 //"EEE MM d hh:mm:ss yyyy"
Tue 02 26 17:46:22 2013 //"EEE MM d HH:mm:ss yyyy"
2013-02-26 12:08:00 +0000 //just logging using NSLog
Hope this helps you
You'll need to set timezone for a NSDateFormatter when you print resulting date like this:
[formatter setTimeZone:[NSTimeZone localTimeZone]];
You need to use as :
NSString *line=#"Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :";
NSDateFormatter *dateFormat=[NSDateFormatter new];
[dateFormat setDateFormat:#"EEE' 'MM' 'd' 'HH:mm:ss' 'yyyy"];
NSDate *logDate = [dateFormat dateFromString:[line substringToIndex:24]];
NSLog(#"logDate->%#",logDate);
If we take the log of the NSdate object that will show the date with respect to the timezone +0000 only. for that we need to format the date to our local timezone. like this..
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:#"Mon Feb 18 09:25:53 2013"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);
thanks to all.. it worked by changing the code this way ..
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
return [dateFormat dateFromString:dateString];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSString to NSDate
I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
but it doesn't work and my date is nil
Input data is in en-GB locale, my device's locale is nb-NO
Any suggestions?
You're missing day in your format:
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
Try to use this function
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I hope this will helps u.
I think your Time Zone is wrong. Just use this code , it will work Perfectly :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 EDT 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date :: %#",date);
It will log Output as :
date :: 2013-01-14 18:00:00 +0000
EDIT :
I found Something for you : NSDateFormatter doesn't parse some timezones
You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .
CET is not recognised
Try this :-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"%#",date);
Hope it helps you
Simply "CET" is not a recognized time zone by NSDateFormatter.
Also the date/tine is over specified, best to not try include the day or week (Mon).
Here is an example that demonstrates working code with a recognized timezone:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog output
date: 2013-01-14 19:00:00 +0000
NSLog(#"abbreviationDictionary: %#", [NSTimeZone abbreviationDictionary]);
does show
CET = "Europe/Paris";
so this looks like an Apple bug in NSDateFormatter.
Report the bug at: Apple Bug Reporter
You can use:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
If you still want to custom your date format try this one:
yyyy-MM-dd HH:mm:ss ZZZ
Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
This is an example result to string from my server:
Sat, 24 Dec 2011 14:33:43 +0000
I'm trying to convert it to NSDate and I keep getting null.
This is my code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0000"];
NSDate *date = [dateFormat dateFromString:dateString];
Why do I get null?
It works fine here. You probably want your format to be this tho:
#"EEE, dd MMM yyyy HH:mm:ss z"
Otherwise it'll fail if the server returns a timezone that's not GMT.
try this
NSString *dateString =#"Sat, 24 Dec 2011 14:33:43 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"%#",date);
Something else must be wrong with your code. Check where you allocate or assign dateString. That might be null to begin with.
Just tried your code with the string you said you're getting from the server and it's working fine. I even tried what mvds said about using [dateFormat stringFromDate:date] that's also returning the correct date
How to get this format of date from NSString;
Wed, 22 Jun 2011 12:36:00 +0100 to Wed, 22 Jun 2011.
Thanks
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
The minimalistic version is E, d MMM y or to specify 2 digit days and 4 digit years E, dd MMM yyyy. The Date Formatter uses the Unicode Technical Standard #35.
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