This question already has answers here:
NSDate is not returning my local Time zone /default time zone of device
(7 answers)
Closed 9 years ago.
I am using the datepicker for picking the time but after sending to the server when I am retrieving back then its showing 5-6 hour difference.
Server hosted in USA.
So how I will do it accurately without any difference, User do request from any where.
Thanks,
Arun
UTC is standard time zone to be used. Following is the code to get date in UTC
+(NSString *)getCurrentTime{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddHH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *dateStr = [dateFormat stringFromDate:date];
RELEASE_OBJECT(date)
RELEASE_OBJECT(dateFormat)
return dateStr;
}
Send the date with the timezone. For example:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [formatter dateFromString:dateString];
Will include the timezone and the server will be able to translate to its own timezone.
We use the ISO 8601 for better compatibility. There are also NSFormatter subclasses that to convert from ISO 8601 to NSDate and back (like this).
It is time zone issue. You can use [yourDate dateByAddingInterval:[NSTimeZone secondsFromGMT]]; .
Please try to set the Locale for the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mm:ss"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate* sourceDate = [dateFormatter dateFromString:dateString];
//Timezones
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
//Interval in Timezones
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//converted date
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSString *strFinalDate = [dateFormatter stringFromDate:destinationDate];
[dateFormatter release];
[destinationDate release];
If you are sending your iPhone default times to server and server is also sending the same time which you have send it then it will be problem of the converting your NSDate to NSString with NSDateFormatter with some different timezone.
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
Use above code when you using NSDateFormatter.
Related
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000
- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.
As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000
NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.
You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
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.
I asked a question not too long ago about timezone and I was using EST. Users suggested me to use EDT. I want to know why I should use one or the other because they both print the same time for me. Here is the code to better illustrate what I mean.
NSDate *today = [NSDate date];
NSDateFormatter *edtDf = [[NSDateFormatter alloc] init];
[edtDf setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EDT"]];
[edtDf setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate = [edtDf stringFromDate:today];
NSLog(#"The EDT is %#", stringDate);
NSDate *today1 = [NSDate date];
NSDateFormatter *estDf = [[NSDateFormatter alloc] init];
[estDf setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EST"]];
[estDf setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate1 = [estDf stringFromDate:today1];
NSLog(#"The EST is %#", stringDate1);
They may print different things depending on the time of year (since time of year determines whether Daylight Saving Time is active).
Don't use EST or EDT. Use US/Eastern or America/New_York:
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"US/Eastern"];
// or
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"America/New_York"];
These time zones adjust for Daylight Saving Time at the correct times of the year.
This question already has answers here:
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
(3 answers)
Closed 8 years ago.
I need to set local timezones as per the location of the ios device.
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
or
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GTC+5.30"]];
is suited?
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(#"User's current time in their preference format:%#",currentTime);
You can get the name of the local time zone using:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
This is how I do it:
NSString *strDateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS";
// ---------------------------------------------------------
// input date is UTC date time, need to convert that time
// to user's own local device timezone to get correct date
// ---------------------------------------------------------
NSDateFormatter *dateFormatterUTC = [[NSDateFormatter alloc] init];
[dateFormatterUTC setDateFormat:strDateFormat];
[dateFormatterUTC setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// get UTC Date first
NSDate* utcDate = [dateFormatterUTC dateFromString:strDate];
// get local date from UTC Date
NSDateFormatter *dateFormatterLocal = [[NSDateFormatter alloc] init];
[dateFormatterLocal setDateFormat:strDateFormat];
[dateFormatterLocal setTimeZone:[NSTimeZone systemTimeZone]];
// get final local date calculated by offsetting UTC Date
NSDate *localDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:utcDate]];
NSDate *localNowDate = [dateFormatterUTC dateFromString:[dateFormatterLocal stringFromDate:[NSDate date]]];
NSString *dateStr = #"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"date : %#",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(#"DateString : %#", dateStr);
- (Courtesy: Yuvaraj.M)
You can use the localTimeZone class method of NSTimeZone class to get a relative time zone object that decodes itself to become the default time zone for any locale in which it finds itself.
SRC: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimeZone_Class/Reference/Reference.html#//apple_ref/occ/clm/NSTimeZone/localTimeZone
If you need latitude and longitude: Get the official time zone database at http://www.iana.org/time-zones. Download tzdata2013g.tar.gz. There's a file in that archive named zone.tab that gives a lat/long for each time zone, for example:
CH +4723+00832 Europe/Zurich
The +4723+00832 indicates latitude = +47º23", longitude = +8º23"
(SRC:Get Timezone/country from iPhone)
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000
- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.
As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000
NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.
You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);