2

How can I convert date with timezone1 to date with device local timezone2?

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Jerusalem"]];
NSDate *date = [dateFormat dateFromString:timestamp];

then something like:
[dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
date2 = [dateFormat2 dateFromDate:date withOtherTimeZone:zone];

UPDATE:

I think I got it.

//get source date from the server
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *sourceDate = [dateFormat dateFromString:timestamp];

//set timezones
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

Is it ok?

assafey
  • 105
  • 2
  • 10
  • You might want to do a little studying first. NSDate does not "have" a timezone. – Hot Licks Jan 09 '14 at 19:00
  • You shouldn't. It's one thing to display a date to the user in different formats and timezones but there is little reason to actually convert an NSDate. Why do you think you need to? – rmaddy Jan 09 '14 at 19:00
  • We have a chat section in our app that receive timestamps from the server. The timestamps are according to server's timezone, and we want to show the user the current time he received the message and not the global server time. This can be reached with the local timezone of the device, am I right? – assafey Jan 09 '14 at 21:46

3 Answers3

8

OK first off, time is time. At this very second, everywhere on earth, its is the same second. Stop thinking of time format as time. It's not. All time should be in GMT, and then you can display it based on TimeZone.

Date *d = [NSDate date] ;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"];
NSTimeZone *nyTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"sometimeabbr."];
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimezone: nyTimeZone];
NSLog(@"ny time is %@" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: d]);

If you are given a string date and need to convert, create NSDateFormatter to ingest the string date. Make sure you set a timezone. Now you have a NSDate object (no timezone in NSDates), that yiou can format into any other timezone you want.

Please for the love all all that is holy. Treat time as seconds that marches forward, and timezones as the formatting of those timezones.

John
  • 2,640
  • 1
  • 16
  • 16
  • 1
    This is the correct answer. The date is the same in the server as in the client, it's just that it is *formatted* for a different time zone when converting it to a string for display. Also, if the response from the server comes as a string with a timezone, the TZ specification will be ignored (which is the right thing to do, as it could potentially be using UTC, so the previous answer would give a wrong result since it is forcing the time to be interpreted as if coming from a certain timezone) – Victor Jalencas Apr 28 '14 at 19:27
1

This is the answer:

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
assafey
  • 105
  • 2
  • 10
  • 1
    Please consider accepting John's answer, as it is the right way to do it. – Victor Jalencas Apr 28 '14 at 19:28
  • This is exactly what I was looking for, thanks! There are many answers on how to display date/time in the user's timezone, but this answer actually creates a new NSDate object with the time as it appears to the user. Handy. – Rogare Nov 14 '14 at 15:49
1

*Different time zone date convert to local time zone date in swift.

let timeZone1 = NSTimeZone(name: timeZone1Name)
let localTimeZone = NSTimeZone.localTimeZone()

let timeZone1Interval = timeZone1?.secondsFromGMTForDate(timezone1Date!)
let deviceTimeZoneInterval = localTimeZone.secondsFromGMTForDate(timezone1Date!)

let timeInterval =  Double(timeZone1Interval! - deviceTimeZoneInterval)

let originalDate = NSDate(timeInterval: timeInterval, sinceDate: timezone1Date!)

let dateFormater : NSDateFormatter = NSDateFormatter()
    dateFormater.dateFormat = "YYYY-MM-dd HH:mm:ss Z"
NSLog("Converted date: \(dateFormater.stringFromDate(originalDate))")
let dateString = dateFormater.stringFromDate(originalDate)
let localTimeZoneDate = dateFormater.dateFromString(dateString)

*

abhi
  • 563
  • 6
  • 9