Unix time stamp received from server using EEST timezone - ios

In my application i request certain date field from my server, the server returns values like :2014-06-03 00:00:00 EEST , but i need to load that data using GMT(GMT+2…) format, i am using the following code :
double unixTimeStamp = [[date objectAtIndex:indexPath.row] doubleValue];
NSTimeInterval _interval=unixTimeStamp;
NSDate *dateToFinal = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
_formatter.dateFormat = #"yyyy-MMMM-dd HH:mm:ss zzz";
[_formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:[[NSUserDefaults standardUserDefaults] objectForKey:#"timeZone"]] ];
[_formatter setDateFormat:#"dd MMMM yyyy"];
dateFinal=[_formatter stringFromDate:dateToFinal];
// NSLog(#"Date Time < 3 %#" , dateFinal);
the user has the possibility to change his timezone, that's why when he saves it it's stored in a local cached variable , which i call in the previous code. But the most weird part is , if the user changes to GMT +1 or +2 or whatever, the application would still output the same hour time, which is 00:00, but if he changes to anything different from GMT, it will change. It's not normal to have GMT,GMT+1,GMT+4… to output the same hour which is 00:00. As a unix timestamp example we have 1401742800, which if u use an online time converter you would get Mon, 02 Jun 2014 21:00:00 GMT , but in the app its neither the same date nor time…what am i missing ?

Related

iOS 'dateFromString' Returning different?

I am using the NSDateFormatter's dateFromString, yet when it is given the string, it spits out different hours.
code
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = #"yyyy-MM-dd";
NSDate *firstDate = [dateformatter dateFromString:#"2017-06-02"];
NSDate *secondDate = [dateformatter dateFromString:#"2016-11-02"];
output
firstDate: 2017-06-01 22:00:00 +0000
secondDate: 2016-11-01 23:00:00 +0000
I wonder why they do not have the same hour?
You did not specify a time for the two dates, so they were initialized to midnight, local time. When you printed them they were displayed in UTC. Your local time zone (Central European Time) observed Daylight Savings Time from March 27, 2016 to October 30, 2016. Therefore the June midnight date, which fell under Daylight Savings, is 2 hours ahead of UTC (22:00:00), while the November midnight date, which did not fall under Daylight Savings, is only 1 hour ahead of UTC (23:00:00).

Using NSDate to convert a string to a date, my dates are changed to 6 months back [duplicate]

This question already has an answer here:
NSDateFormatter dateFromString returns date with wrong month
(1 answer)
Closed 6 years ago.
I have an web service that spits out some dates, and in my iOS app, I'm converting UTC date to local date. I have verified that the web service is spitting out UTC dates and that iOS recognizes it as UTC.
Once i have converted my web service JSON to an NSMutableArray, set a break point and type po [listOfTasks valueForKey:#"LASTEMAILDATE"]
Results
<__NSArrayI 0x157068c80>(
,
7/28/2016 2:01:41 PM,
7/28/2016 2:01:39 PM,
7/28/2016 2:01:42 PM
)
Now, i do a for loop
for (int i = 0; i < listOfTasks.count; i++) {
FireStormCategories *cat = [listOfTasks objectAtIndex:i];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"mm/dd/yyyy hh:mm:ss a"];
NSDate *date = [df dateFromString:cat.LASTEMAILDATE];
MNLog(#"%# was changed to %#", cat.LASTEMAILDATE, [df stringFromDate:date]);
}
that log spits out
was changed to (null)
7/28/2016 2:01:41 PM was changed to 01/28/2016 02:01:41 PM (expecting: 7/28/2016 10:01:41 AM )
7/28/2016 2:01:39 PM was changed to 01/28/2016 02:01:39 PM (expecting: 7/28/2016 10:01:39 AM )
7/28/2016 2:01:42 PM was changed to 01/28/2016 02:01:42 PM (expecting: 7/28/2016 10:01:42 AM )
my first object has no date, the (null) is expected, however, this is where I'm confused. my other 3 dates are changed to a date that reflects 6 months ago and the hours should go back 5 (EST).
You are using wrong format for month. MM is used for month and mm is used for minutes
[df setDateFormat:#"mm/dd/yyyy hh:mm:ss a"];
should be
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
The 01 you are getting is the minutes from the date.

Issues in converting date string (any time zone) to NSDate of French Time Zone

I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTC (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.

Is it possible to convert input string with any format into date ios?

Is it possible to convert input string with any format into date?
I want to receive ate and time from textfield with any format like date: may 22 2000, wed may 20, may, 30 2000 etc.. and also need to get correct value according to the local timezone.
please help?
NSDateFormatter can help convert any type of date NSString to NSDate. The important thing is to use the right format for the date.
Here is some code you can try
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
You will have to set the date format to whatever format your string is in. Here are some date format specifiers: (Complete list here)
eeee - Local day of week spelled out
yyyy - year (4 digits)
MMMM - Month spelled out
dd - day of month with no leading zeros
HH - hour of day (24 hour format)
mm - minutes of hour (with leading zero)
Edit
The date format for the following dates will be:
May 03 2000 : "MMM dd yyyy"
Mon, Jan 03 : "eee, MMM dd"
Mon 31 Jan : "eee dd MMM"

Validation of date and month

i have a current date using NSDate which is ma start date....and i add 4 more days to the current date where i get ma endDate..
NSString *StrtDate= [dateFormatter stringFromDate:[NSDate date]];
int daysToAdd = 4;
NSDate *newDate1 = [[NSDate date] dateByAddingTimeInterval:60*60*24*daysToAdd];
NSString *StopDate= [dateFormatter stringFromDate:newDate1];
suppose the current date is todays date..thats 28th of jan the end date becomes 32nd of Jan which is invalid rite?
how do u validate the date??
Have you run the code and checked whether the resultant date is indeed 32nd January?
The framework is intelligent enough to understand valid dates. You will get correct date.
You don't need to worry about this as the date framework knows how many days are in each month.

Resources