Current Time in Date format shows wrong - ios - 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.

Related

Converting calendar date to the correct date

I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);

iOS Objective C: Date format returns nil

In my iOS application(Objective C) I am adding date (29th Nov 2016) in database in following format:
2016-11-29 04:08:00 PM
And during displaying it I want to show in following format:
11/29/2016 04:08:00 PM
For this I have added formator like
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter stringFromDate:date1];
But here newDate returns nil.
How can I change the date format? Please suggest me.
Thank you.
I have tried your code with below changes:
NSString *StartDateTime = #"2016-11-29 04:08:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter1 stringFromDate:date1];
NSLog(#"%#",newDate);
Here with in your code I have added a new object of NSDateFormatter (For above example dateFormatter1) with new allocation and new date formate.
You will get your answer with this way.
Make sure every time that you have to create new instance of NSDateFormatter every time you want to change the DateTime formate.
And even after trying this option, If you don't get your desired result then please try to run the code in Device, not in Simulator. Because simulator may not return accurate result all the time especially for date and time

NSDate get local time

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.

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