-1

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

Muhammad Irfan
  • 280
  • 3
  • 17
  • What exactly does the last line print? Please add the actual output because most of those errors are not really errors but a bad reading of the output. – Sulthan May 27 '15 at 11:40
  • 2015-05-27 01:00 instead of 2015-05-27 03:00 and time zone offset is +0000 – Muhammad Irfan May 27 '15 at 11:46

4 Answers4

2

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
Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

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.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

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]

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Basically, I want to convert a date from current time zone to French time zone. All I have is a date string. But when I convert the string to date it is returned W.R.T GMT which is different from the current time zone hence creating a difference in my final result. I will be very glad if you can help me in this scenario. – Muhammad Irfan May 27 '15 at 13:15
  • @MuhammadIrfan You are wrong, I repeat. The date is not returned in any time zone but NSLog converts it to a string in GMT. You have to take the `currentDate` and run it through a `NSDateFormatter` with French time zone. – Sulthan May 27 '15 at 13:30
  • @MuhammadIrfan Please, update your question, don't paste code to comments. – Sulthan May 27 '15 at 13:51
  • I have edited the question. Kindly take a look at it now – Muhammad Irfan May 27 '15 at 14:19