I have a method,
+ (NSDate *) convertToDateFrom:(NSString *) dateString
{
if (dateString == nil || [dateString isEqual:#""]) return nil; //return nil if dateString is empty
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMMM yyyy HH:mm"];
NSDate *date = [df dateFromString:dateString];
return date;
}
When I pass,
#"Monday, 21 November 2011 17:01" //Passed string
It returns a wrong date,
2011-11-21 23:14:00 +0000 // Output
I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date.
Am I doing something wrong?
Thanks
The +0000 at the end of the date indicates GMT. All dates are stored relative to GMT; when you convert a date to a string or vice versa using a date formatter, the offset to your time zone is included. You can use NSDateFormatter's -setTimeZone: method to set the time zone used.
In short, you're not doing anything wrong in your code. Use [df stringFromDate:date]; to see that the date is correct. (You can also use NSDate's -descriptionWithCalendarFormat:timeZone:locale:.)
try using
df stringFromDate:date
Following worked on mine,
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [df stringFromDate:date]);
gave me output as :
Date for locale en_US: Wednesday, 26 June 2013 15:50
Try setting the time zone and locale.
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
Related
I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
This question already has answers here:
Convert NSDate to NSString
(19 answers)
Closed 8 years ago.
I have an NSDate Object which is in 12 hour format like 2014-09-16 04:40:05 pm +0000.
I want to convert this into 24 hour format and want to get back an NSDate object like 2014-09-16 16:40:05 +0000.
Can some one guide me in doing that.
So i want some method like :
-(NSDate *) get24HourFormat:(NSDate *) date{
return date object;
}
Simple use this:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
dateformate.dateFormat = #"HH:mm a"; // Date formater
NSString *timeString = [dateformate stringFromDate:[NSDate date]]; // Convert date to string
NSLog(#"timeString :%#",timeString);
Logic: Simply convert date format hh:mm to HH:mm
There seems to be a deep misunderstanding here what NSDate is and does.
An NSDate object is a point in time in UTC. It doesn't have a time zone. It doesn't have a format. It has nothing. You can't change its format, it doesn't even make any sense.
What you can do is to use an NSDateFormatter to convert the NSDate to a string. By default, NSDateFormatter uses your local time zone, which means for most people that the result will be different from the result that NSLog would show for an NSDate. In the NSDateFormatter, you can use whatever settings you want.
Usually you would respect how the user set up his date formatting and not change it for anything that is visible to the user. As a user, if I had set up my device to show days in 12 hour format, I'd be very annoyed if your application worked differently.
try this..
-(NSString *)changeformate_string24hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
return [df stringFromDate:wakeTime];
}
-(NSString *)changeformate_string12hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
return [df stringFromDate:wakeTime];
}
I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);
I want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet. Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:dateString];
return date;
I've set up my dateFormat based on all the solutions I've read to solve this problem, but none of these settings have solved by problem. The systemLocale is definitely set up for English so that should not be causing any issues.
This is the dateString I'm passing to dateFromString:
Wednesday, October 9, 2013 at 2:40:29 PM Pacific Daylight Time
Thanks for the help!
There are two issues here:
The format of date string the formatter is expecting (#"yyyy-MM-dd hh:mm:ss") is different from the format of the date string you're trying to parse (#"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz").
Setting the formatter's locale to [NSLocale systemLocale] is causing [dateFormat dateFromString:] to return nil. Set it to [NSLocate currentLocale].
The full code for the formatter should be:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:dateString];
Yet another way to get nil is if you use hh and your hours are on the 24 hr clock and > 12, in that case, you need HH (or H, for zero-padded).
That is:
Format: yyyy-MM-DD hh:mm:ss, string: "2016-03-01 13:42:17" will return nil
Format: yyyy-MM-DD HH:mm:ss, string: "2016-03-01 13:42:17" will return the date you expect.
Hat-tip to #neilco (see comments below his answer) for this. If you like this answer, please up-vote his, too.
According to NSDateFormatter documentation :
When working with fixed format dates, such as RFC 3339, you set the
dateFormat
property to specify a format string.
If your date format is 2017-06-16T17:18:59.082083Z then dateFormat property should look like this yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ.
Swift 3
let dateFormatter = DateFormatter()
let date = "2017-06-16T17:18:59.082083Z"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
let result = dateFormatter.date(from: date) // 2017-06-16 17:18:59 +0000
Your date format doesn't match the string that you're passing, your dateString should be in this formate as per your [dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
2013-10-09 02:40:29
nil means dateFormat object was unable to parse your string.
In case anybody is stuck on the same hilarious edge case as me:
"2020-03-08T02:00:00" will return nil as long as you're in a locale that follows Daylight Savings Time, because that hour is skipped and simply doesn't exist.
You're trying to use dateFromString but the Format you have passed to your Formatter is different of what you're using in dateString.
Try to use this config in your dateFormat: E',' M d',' yyyy 'at' hh:mm:ss aa z
Don't forget to escape "yyyy/MM/dd' 'HH:mm:ss" space symbols
Team,
I am comparing the date which is formed from string using NSDateFormatter with the iOS system date. The below statement returns true when the system date time settings is set with 24-Hour Time ON, but the same code returns false when 24-Hour Time OFF.
Problematic Code:
if ([(NSDate*)[NSDate date] compare:currDate] == NSOrderedAscending) {
// -- Code -- This is executed only when the 4-Hour Time ON
}
I am confused. The string using which I am getting the date is in 24 hours format. Is this a problem? Or anything else?
Date Formatting Code:
-(NSDate *)getDateFromString:(NSString *)dateString{
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:#"dd MMM yyyy hh:mm:ss"];
[fmt setTimeZone:[NSTimeZone systemTimeZone]];
[fmt setFormatterBehavior:NSDateFormatterBehaviorDefault];
return [fmt dateFromString:dateString];
}
See Apple's Technical Q&A 1480.
You need to set the date formatter's locale to the special locale of en_US_POSIX. You also need to specify a 24-hour hour format - HH, not hh.
-(NSDate *)getDateFromString:(NSString *)dateString{
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:posix];
[fmt setDateFormat:#"dd MMM yyyy HH:mm:ss"];
return [fmt dateFromString:dateString];
}
23 May 2013 15:37:00 is a 24 hour format string.So the correct date is obtained using the 24 hour format date formatter .Thats it
So use
[fmt setDateFormat:#"dd MMM yyyy HH:mm:ss"];