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.
Related
I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);
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);
The below code should work. What ever the timezone you are in it will always display the time in UTC.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"UTC"]];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"%#",dateString);
Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.
The links again, just in case
objective-c: Conversion between TimeZones
GMT timezone conversion in objective c
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = #"HH:mm:ss";
NSString *dateString = [timeFormatter stringFromDate: localDate];
I was trying to format a time from GMT+7 to GMT+3:
I am building an app with a world clock in specific country (the user will be at the GMT+7and I want to represent the GMT+3 time )
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
NSLocale *USLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:USLocale];
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
I looked deep into NSDate class reference but I didn't understand how to make it.
Please if someone can help me I will be grateful.
There is 2 important parameters that works separately: Time and Time Zone.
e.g: Vietnam uses GMT+7
If I know that the time in Vietnam is 9:00 AM, then GMT time is 2:00 AM.
When you get the Date from your device you are getting Time and Time Zone: YYYY-MM-DD HH:MM:SS ±HHMM. Where ±HHMM is a time zone offset in hours and minutes from GMT.
Usually you are only using time. However with NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT"] you can tell the NSDateFormatter that you want the GMT time related to your local Time Zone. So, with:
NSDateFormatter *dt = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
[dt setTimeZone:timeZone];
You can get the GMT date of your local time zone date.
So, If you have GMT+7: 9:00 AM and you want to print out GMT+3: 5:00 AM, you have 3 possibilities:
NSDate *localDate = [NSDate date];
OPTION 1
Add a time interval of -4 hours:
NSTimeInterval secondsInFourHours = -4 * 60 * 60;
NSDate *dateThreeHoursAhead = [localDate dateByAddingTimeInterval:secondsInFourHours];
NSDateFormatter *dt = [[NSDateFormatter alloc] init];
[dt setDateFormat:#"h:mm a"];
NSLog(#"GMT+7(-4) = %#", [dt stringFromDate:dateThreeHoursAhead]);
This is the easiest way to do it. If you are always at GMT+7 and you need GMT+3, this is a time interval of -4 hours.
OPTION 2
Set the time to GMT time zone and then add a +3hours time interval. The easiest way to do it is to add the 3 hours first and then move the time to GMT:
NSTimeInterval secondsInThreeHours = 3 * 60 * 60;
NSDate *dateThreeHoursAhead = [localDate dateByAddingTimeInterval:secondsInThreeHours];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"h:mm a"];
NSString *date = [dateFormatter stringFromDate:dateThreeHoursAhead];
NSLog(#"GMT+3 = %#", date);
OPTION 3
This is the better option. GMT+3 is EAT (East Africa Time) you can set your time zone to EAT with: [NSTimeZone timeZoneWithName:#"EAT"]
NSDateFormatter *dt = [[NSDateFormatter alloc] init];
[dt setDateFormat:#"h:mm a"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"EAT"];
[dt setTimeZone:timeZone];
NSLog(#"EAT = %#", [dt stringFromDate:localDate]);
Option 3 is always retrieving GMT+3
An example code here.
I am trying to convert my device current time into Zone (America/New_york)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
NSDate *start = [NSDate date];
start now has the device current time in IST. How can I convert it into zone 'America'
I planned to do the below
start = [dateFormatter dateFromString:<start date String>];
A Day is categorised into 3 divisions based on a time period.
Say 08:00 - 15:00 AS PHASE1
15:00 - 17:00 AS PHASE2
17:00 - 20:00 AS PHASE3
20:00 - 08:00 AS TRANSITION PHASE and can be ignored.
Am just taking device's date and append the time string to identify in which phase it is.
Example: Indian Time is 05-FEB-2014 01:25 AM (Device Date)
Considering 15:00 as first cutoff.. I append the '05-FEB-2014' to 15:00 and digest it as American timezone.. Which resolves and gives me 06-FEB-2014 02:30 IST(Cut off Date).. So the difference between the both days goes greater than one day!
My expected result could be take device time also in American Timezone and compare with the compare with the nearest cut off time.
NSDate does not have a time zone. It's a single time reference that's valid anywhere. It's basically just an object wrapper for NSTimeInterval-- all it stores is the number of seconds since a reference date. Time zones only apply when converting to/from user-visible strings.
You can get a user-visible string from your date formatter as
NSString *localizedDateString = [dateFormatter stringFromDate:start];
But converting NSDate to a particular time zone is not a meaningful goal. There's no time zone on NSDate, so it's not a conversion that can be applied.
Date of new york :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"America/New_York"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Date : %#", dateString);
Date of all zones :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
for (NSString *name in timeZoneNames) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:name];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"timeZone abbreviation : %#\nName : \"%#\"\nDate : %#\n\n", [timeZone abbreviation], name, dateString);
}
See the stringFromDate Method in the dateformatter. You pass in an NSDateand it converts to the specified format.
UPDATE: Here is how you set the current time zone [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
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.