It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString
from a NSDate
which represents the same time on every device, independently from the iPhone's time zone settings.
Suppose userA
is in London, where the actual time is 14:00, userB
is in New York where is 9:00 and userC
is in Hong Kong, where the actual time is 21:00.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
Actually with this code I'm getting these results (when I log the dateString
):
- userA:
08/12/14 14:00:00
- userB:
08/12/14 09:00:00
- userC:
08/12/14 21:00:00
But I need to create dates like this
- userA:
08/12/14 14:00:00
- userB:
08/12/14 14:00:00
- userC:
08/12/14 14:00:00
My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s
.
Is it possible to get the NSDate *now = [[NSDate alloc] init];
from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.
I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"Europe/London"];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(@"the date is %@,", dateString);