Deviation from expected output with NSString to NSDate conversion using NSDateFormatter - ios

I am using the below method to convert a NSString to NSDate.
Always when I construct the NSDate from String, the date is one day behind the current day I have provided as part of the input and hour is 18:30:00 +0000. Why this deviation from what I have provided. I was expecting to have the same date what I have provided and hour as 00:00:00 +0000
+(NSDate*)convertStringToNSDate:(NSString*)string withFormat:(NSString*)format{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:format];
NSDate *date = [dateFormat dateFromString:string];
[dateFormat release];
return date;
}

This question comes up quite regularly but I could not find a suitable duplicate (searching on the phone does not help).
NSDate represents a specific point in time. When you log the value of an NSDate it is displayed in GMT, which is 5.5 hours behind your timezone (India, I assume). So the value is correct. If you run that date back through your date formatter you will get the local time of midnight again, since the date formatter is using your local time zone.

Related

Convert 12 hr to 24

I'm converting 12 hour date to 24 here, but it failed to get perfect time.
Here is code :
NSString *dateStr = #"2016-08-12T04:10:14.915Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormat dateFromString:dateStr];
and my final date is
2016-08-11 22:40:14 +0000
How ?
The NSDate object that you are getting is GMT. In France GMT -2, if I run your code I have a time of 02:10.
Am I correct assuming your GMT offset is -5:30 ?
NSDate objects don't have time zones; they represent an absolute
moment in time. However, when you ask one for its description (by
printing it in an NSLog, e.g.), it has to pick a time zone. The most
reasonable "default" choice is GMT. If you're not in GMT yourself, the
date will seem to be incorrect, by the amount of your own offset.
You should always use an NSDateFormatter, setting its timezone to
yours, before displaying a date.
Don't trust what NSLog or the debbuger are telling you about a NSDate.
use
NSString dateAsString = [dateFormat stringFromDate:date];
to check your date :)

NSDateFormatter showing wrong date in ios? [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 6 years ago.
i have NSString with this date format "2016-03-16" and i added following code to get the same date in proper NSDate format but its returning " 2016-03-15 18:30:00 +0000 ". How do do i get same "2016-03-16" in NSDate ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = [dateFormatter stringFromDate:dateFromString];//2016-03-16
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"%#----",dateValue); //" 2016-03-15 18:30:00 +0000
Your local time zone is presumably UTC+5:30. You are specifying a date but not a time, so the time is implied to be midnight. Midnight on the 16th in your local time zone is 18:30 the day before (the 15th) in UTC time, which is why you get "2016-03-15 18:30:00 +0000"
When you log the date with NSLog(#"%#----",dateValue) you are actually invoking [dateValue description], which displays the date using UTC.
You can use NSLog(#"%#----",[dateValue descriptionWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone] locale:nil]) and you will see the date in your current time zone.
Be aware though that the description and associated methods such as descriptionWithCalendarFormat methods are only for debugging, you should use an NSDateFormatter to convert dates to strings. iOS_Binod's answer shows one way you could do this.
You try to print NSDate instance in console. That's reason your code print default format value in console.
You need to get string value from NSDate instance with the help of this method [your_dateformater stringFromDate:dateInstance]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = #"2016-03-16";
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"string convert into Date is - %#",[dateFormatter stringFromDate:dateValue]);
The date formatter uses your local time zone by default. The -[NSDate description] method (which is what %# calls) uses UTC. This is why the strings are different.

Convert NSString to NSDate Using NSDateFormatter

trying to convert NSString to NSDate
NSString *startDateForCal='09-03-2016 08:00:00 AM'
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd-MM-yyyy HH:mm:ss a"];
NSDate * EventStart = [dateFormatter dateFromString: startDateForCal];
But getting mixed result as "2016-03-09 18:30:00 +0000" returning date as correct but time didn't match with string It would be welcome any suggestions
Thank you
it should be
[dateFormatter setDateFormat: #"dd-MM-yyyy hh:mm:ss a"];
i.e. no capital HH for hour. HH return the Hour in 24 format
For more details Click now
and regarding the error in date,
since you have set the time format wrong( as HH instead of hh), it took the time as 12:00 AM and showed it in GMT timezone(IST - 5 and half hours, so in your case 12:00 AM - 5:30 = 18:30 of previous day), i guess you haven't set the locale properly.
setting the locale(in swift),
dateFormatter.locale = NSLocale(localeIdentifier:"en_IN")
Your question makes no sense. Your goal was to obtain an NSDate from an NSString. You did that, and you did it successfully.
How you discover what that NSDate is, is a completely different matter. It is represented to you by default (in the console) in a certain time zone and format. But that is irrelevant; it is still the same date, and it is the correct date.

NSDateFormatter return Date in GMT specific timezone

I want to get the date and time in a specific time zone. I am getting most of the things right but just at the end when i get the date from NSString using NSDateFormatter method it returns me the date in the GMT specific time zone. The method [formatter stringFromDate:gmtDate]; return me the expected date and time. The problem happen when i get the date from the string i-e when i execute this method self.localTime = [formatter dateFromString:str];. self.localTime is a NSDate property in my class.
So when i print the str it gives me the date and time in that specific time zone which is represented as self.timeZoneID, which is also a property on my class
NSDate *gmtDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:self.timeZoneID]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *str = [formatter stringFromDate:gmtDate];
NSLog(#"Date string : %#", str);
self.localTime = [formatter dateFromString:str];
Any idea that what could be the reason that i am getting the right string output but when i assign it to my property localTime it give me the time in GMT
Reason is that NSDate have a default time zone that is GMT for consideration and when a user wants to have time specific for some time zone then NSDateFormatter provides way to set specific time zone which you are using for gmtDate object and not for self.localTime(this is taking GMT ,default time zone).

Problems converting a string date to NSDate

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

Resources