Iphone date format - ios

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.

Related

iOS 9 : Add GMT Time Value

I had search on google how to get GMT Time in Objective-C, and I got this :
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];}
Is there any way to add or set GMT Time ? (GMT+5)
The most flexible way is to use timeZoneForSecondsFromGMT. This is more typo proof than using strings like GMT+X.
Here is a fuly working example:
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:(60*60*5)];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];
}
It returns 2016-08-29T12:13 (when GMT time is 2016-08-29T7:13)
Note: 60*60*5 means 5 hours X 60 minutes X 60 seconds
Can try like this.
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
If you create NSDate instance then it will return date in UTC or GMT format by default.
Now when you convert this date to string by any date formatter then returned string (date) will be in local timezone (i.e. device's time zone).
You can create custom timezone with it's name to get date in that timezone.
NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(#"current date in gmt : %#",date);
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateInStrFormat = [df stringFromDate:date]; // this date(string format) will be in current timezone that was set in your device and
NSLog(#"current date in local or device's default timezone : %#",dateInStrFormat);
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
NSLog(#"timezone : %#",timeZone);
[df setTimeZone:timeZone];
NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone
NSLog(#"date in custom timezone : %#",dateInCustomTimeZone);
NSLog(#"timezone : %#",timeZone); will print something like, Asia/Kolkata (IST) offset 19800. 19800 is offset, if you divide it with 3600 then you will get difference with gmt like (+/- 5.30 etc)
Link for different timezone names
Or you can got timezone like,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
NSLog(#"timezone : %#",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(#"timezone : %#",timeAone2);

iOS: What should I use EST or EDT and why?

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.

Current time in UTC in YYYYMMDDHHMMSS format

I want current time in UTC format.I am using this code
NSDate *myDate = [NSDate date];
NSLog(#"myDate = %#",myDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSLog(#"Time zone = %#",timeZone);
[dateFormatter setDateFormat:#"yyyyMMddHHmmss"];
[dateFormatter setTimeZone:timeZone];
NSString *localDateString = [dateFormatter stringFromDate:myDate];
NSLog(#"localDateString = %#",localDateString);
But the timezone showing is GMT.
the loged result is
myDate = 2014-05-30 05:37:39 +0000
Time zone = GMT (GMT) offset 0
localDateString = 20140530053739
Any one know why this happening.
Thanks in advance.

Convert Date with TimeStamp into NSString iOS

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

Date and Time issue in ios [duplicate]

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.

Resources