I am definitely not new to NSDateFormatter or NSDate but this has completely stumped me. How in the world am I getting fire date wrong?
NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
[firedateFormat setDateFormat:#"hh:mm a EEE dd MMM, YYYY"];
[firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *fireDate1 = [firedateFormat dateFromString:#"5:30 AM Sat 27 Oct, 2012"];
NSLog(#"fireDate1: %# ...", fireDate1);
OUTPUT:
fireDate1: 2011-12-31 10:30:00 +0000 ...
It was a smaller error, switch the YYYY to yyyy
NSDateFormatter *firedateFormat = [[NSDateFormatter alloc] init];
[firedateFormat setDateFormat:#"hh:mm a EEE dd MMM, yyyy"];
[firedateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *fireDate1 = [firedateFormat dateFromString:#"5:30 AM Sat 27 Oct, 2012"];
NSLog(#"fireDate1: %# ...", fireDate1);
Output: fireDate1: 2012-10-27 10:30:00 +0000 ... (Date in GMT)
A great reference for all Date Formatters http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/
Related
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
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.
Hi I have a string that is like this: Thu, 09 Jun 2011 5:00 pm CST, where CST could be any time zone or something.
Help I tried this already but it's null:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:#"EEE, dd MMM yyyy h:mm z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *time = [formatter2 dateFromString:#"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(#"%#",time); //null
Am I doing somethings wrong? Help me I'm new.
You aren't accounting for the AM/PM. I believe that that is 'a'.
try:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:#"EEE, dd MMM yyyy h:mm a z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *time = [formatter2 dateFromString:#"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(#"%#",time); //null
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"
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);