NSDate get local time - ios

I build app that get date string(date in french time) from a database. after i get the string i convert it to nsdate:
NSString *dateStr = "20/3/2016 21:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm zzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
self.date = [dateFormatter dateFromString:dateStr];
Into self.date i get(if i print the object) :
2016-06-10 21:00:00 +0000
But when i try to convert it to local time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSString *time = [formatter stringFromDate:cellItem.date];
I get a different time: 00:00
Any idea what can be the problem?

The default time zone for an NSDateFormatter is the time zone of the device, and as you didn't set it explicitly in the second formatter, that is what will be used.
Use the first formatter for both jobs or set it explicitly as you did in the first formatter:
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
Also a note on:
Into self.date i get (if i print the object)
If you are using NSLog() or the debugger to view an NSDate object, it will use [NSDate description] to generate the string, which will always use the GMT time zone. This constantly trips people up.

Related

iOS 9 : Add GMT Time Value

I had search on google how to get GMT Time in Objective-C, and I got this :
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];}
Is there any way to add or set GMT Time ? (GMT+5)
The most flexible way is to use timeZoneForSecondsFromGMT. This is more typo proof than using strings like GMT+X.
Here is a fuly working example:
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:(60*60*5)];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];
}
It returns 2016-08-29T12:13 (when GMT time is 2016-08-29T7:13)
Note: 60*60*5 means 5 hours X 60 minutes X 60 seconds
Can try like this.
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
If you create NSDate instance then it will return date in UTC or GMT format by default.
Now when you convert this date to string by any date formatter then returned string (date) will be in local timezone (i.e. device's time zone).
You can create custom timezone with it's name to get date in that timezone.
NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(#"current date in gmt : %#",date);
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateInStrFormat = [df stringFromDate:date]; // this date(string format) will be in current timezone that was set in your device and
NSLog(#"current date in local or device's default timezone : %#",dateInStrFormat);
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
NSLog(#"timezone : %#",timeZone);
[df setTimeZone:timeZone];
NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone
NSLog(#"date in custom timezone : %#",dateInCustomTimeZone);
NSLog(#"timezone : %#",timeZone); will print something like, Asia/Kolkata (IST) offset 19800. 19800 is offset, if you divide it with 3600 then you will get difference with gmt like (+/- 5.30 etc)
Link for different timezone names
Or you can got timezone like,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
NSLog(#"timezone : %#",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(#"timezone : %#",timeAone2);

Current Time in Date format shows wrong - ios

I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.

NSDateFormatter stringFromDate: returning local time instead of UTC / GMT

inside my iOS App I have an NSDate Object that should be converted to a NSString showing date and time in UTC / GMT:
NSDateFormatter * dateTimeFormatter = [[NSDateFormatter alloc] init];
[dateTimeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
// OR [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// OR not specify the TimeZone at all...
NSString *result = [dateTimeFormatter stringFromDate:date];
When I set a breakpoint and examine date it is correctly displayed as __NSDate * 2015-08-25 14:03:57 UTC. However the resulting string is 2015-08-25 16:03:57, which is the same date in my local TimeZone (CEST).
No matter which solution I tried to get a string in UTC /GMT format 2015-08-25 14:03:57, I always get the local version 2015-08-25 16:03:57.
All sources I found state, that this should work. Where is my mistake?
This code should works:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[formatter setTimeZone:timeZone];
NSDate *now = [NSDate date];
NSString *dateAsString = [formatter stringFromDate:now];
The only strange thing I see in your code it's that you use dateFormatter instead of dateTimeFormatter when set the NSTimeZone.

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

Date Format changing while printing description

I am getting some date in this format "04/11/13 19:37:00" I want to convert this date to local time zone, Below is the code i am using.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"date value %#",date);
The Problem i am facing is i am using debugger when i place mouse on the "date" variable it is showing "2013-04-12 01:07:00 IST" This is the correct out put i need. But when i am printing the same "date" variable using NSLog i am getting "2013-04-11 19:37:00 +0000". How can i get the correct format can any one explay, Do i need to change the dateformat.
NSLog shows NSDate objects in a fixed format. If you want to log the date in a specific format, convert the NSDate to an NSString using an NSDateFormatter and then log the string.
Remember, logging a date is only for debugging purposes. The format in the log really doesn't matter.
If you want to convert the dateString which was in UTC to Local Timezone, you need to do it in two steps.
1.First form the date by setting the corresponding TimeZone to dateFormatter
2.Set the Local TimeZone to dateFormatter, and form dateString from date
NSString *dateString = #"04/16/13 11:12:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate* date = [dateFormatter dateFromString:dateString];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"%#",localDateString);

Resources