Problems converting a string date to NSDate - ios

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]);

Related

Issue with formatting date

I know this question has been asked so many time and may be duplicate of some question, actually i am trying this for storing Date into array by converting them in String. I need that Value in NSDate format so i again convert that stored string into Date.
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSString *date = [dateformat stringFromDate:datePicker.date];
[kAppDelegate.glbArrName addObject:date];
But I get this output :
NSString *date = [kAppDelegate.glbArrDate objectAtIndex:indexPath.row];
NSLog(#"Date of birth %#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSDate *birthDate = [[NSDate alloc] init];
birthDate = [dateFormatter dateFromString:date];
NSLog(#"Date of birth after formatting %#",birthDate);
Output is:
Date of birth 04-05-16-06-38-14
Date of birth after formatting 2016-04-05 01:08:14 +0000
Why it changes format, as i have done same as previous. please help me find out ..
You're rewriting the date string back into an NSDate, then printing out the NSDate, which defaults to the latter 2016-04-05 01:08:14 +0000 format as part of NSDate's -description.

NSDate - dateFromString is returning nil

I have an NSString that has a date. I'm trying to convert that date to an NSDate. When I do that I get nil fro the NSDate. Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"dateString = %# date = %#", dateString, date);
Here is the output of the NSLog:
dateString = 2015-06-16 date = (null)
What am I doing wrong, and how can I fix it?
The date format does not match the date string. It needs to be: :#"yyyy-MM-dd"
The order and other characters need to match.
yyyy for a four digit year
MM for a two digit month
dd for a two digit day
See: ICU Formatting Dates and Times
Note: NSLog() uses the NSDate description method which presents date/time referenced to GMT (UTC) and NSDateFormatter defaults to your timezone so the date displayed may be different.

How to convert a string Date to NSDate in iOS

I googled so many times, but I am not satisfied with given answer. Please anyone can give correct answer what I need.
This is the retrieve date string from DB : 2015-05-27 10:19 (yyyy-MM-dd HH:mm)
I want to convert into NSDate.
My code is like Below:
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
NSLog(#"date == %#",date);
But output is : date == 2015-05-27 04:49:00 +0000
Its showing 04:49:00 time , but my retrieve time is 10:19.
How can i retrieve perfect time from DB.
Please help out me..
Just convert your date to GMT time like this:-
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
And the output is :-
date == 2015-05-27 10:19:00 +0000
And to get the date without +0000, you can store it in NSString directly:-
NSString *s = [dateFormat stringFromDate:date];
Output :-
2015-05-27 10:19
You are facing the timezone issue with conversion, because server time and local timezone may have difference. So handle this you need to set the timezone to your dateformatter like
[dateFormater setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Convert NSDate in12 Hour Format To 24 Hour Format [duplicate]

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];
}

Creating NSDate object with specific time zone

I've written a method that accepts a NSDate object and should turn it into a NSDate object with EST time zone. The way I've seen other people accomplish this is by using a date formatter to change the date to a string with the specified time zone. The issue is when I try to change that string back to a NSDate object using "dateFromString".
- (NSDate*)turnToEST:(NSDate*)date
{
NSLog(#"turnToEST called with date %#", date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EST"]];
NSString *stringFromDate = [dateFormatter stringFromDate:date];
NSLog(#"date formatted is %#", stringFromDate);
NSDate *dateFromString = [[NSDate alloc] init];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateFromString = [dateFormatter2 dateFromString:stringFromDate];
NSLog(#"date has been changed to %#", dateFromString);
return date;
}
With output...
turnToEST called with date 2013-12-19 14:15:17 +0000
date formatted is 2013-12-19 09:15:17
date has been changed to 2013-12-19 14:15:17 +0000
I'm not sure why this is any different than
Converting NSString to NSDate (and back again)
NSDate values do not have an associated time zone, they represent an abstract moment in time. So "a NSDate object with EST time zone" isn't a thing that exists. Time zones only come into play when formatting them for output, or trying to do calendar-based math.
NSLog always uses UTC when printing its output.
So you're taking a moment in time, formatting it to a string in a particular time zone, and then parsing it back into the same moment in time. That's working as intended.

Resources