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);
Related
My date and time is 20-Nov-2019 21:09 Which is in UTC 24 hours format. now I want to convert it into local time in 12 hours formate. 30-Nov-2019 08:00 AM like this.
My code is :
// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
My code when i send my local time 12 formate into 24 hours UTC
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
// NSLog(#"%#", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = twelveHourLocale;
NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
// NSLog(#"dateString %#", dateString);
return dateString;
}
-(NSDate *)getUTCDate:(NSString *)currentDate{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:currentDate];
if (date1 == nil){
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm a"];
date1 = [dateFormat dateFromString:currentDate];
}
return date1;
}
I think you are doing too much. The format "hh" is the hour in 12-hour format, HH is 24-hour format. You should not have to set the locale for that (though setting to en_US_POSIX does avoid the user's 24-hour preference in the [NSLocale currentLocale] instance which can override that on iOS).
NSDate is an absolute instance in time. You need to apply a calendar and time zone with an NSDateFormatter to get numeric year/month/day etc. values out of it, but you don't need to adjust the offset to the reference date (that is changing the actual date, not just reformatting it in a different time zone).
NSString *utcString = #"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
formatter.dateFormat = #"dd-MMM-yyyy HH:mm";
NSDate *date = [formatter dateFromString:utcString];
formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = #"dd-MMM-yyyy hh:mm a";
NSLog(#"date: %#", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM
I have Issue in dateformatter. I add my code as below:
NSDate *currentDate = [NSDate date];
NSLog(#"Current Date: %#",currentDate); //Current Date: 2016-10-14 08:07:15 +0000
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
NSLog(#"Current TimeZone: %#",tzName); //Current TimeZone: Asia/Kolkata
NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSString *SyTZName = [systemTimeZone name];
NSLog(#"System TimeZone: %#",SyTZName); //System TimeZone: Asia/Kolkata
NSString *strCurrentDate = [NSString stringWithFormat:#"%#",[NSDate date]];
NSLog(#"Current Date: %#",strCurrentDate);//Current Date: 2016-10-14 08:07:47 +0000
NSDateFormatter* localDateFrm = [[NSDateFormatter alloc] init] ;
[localDateFrm setTimeZone:[NSTimeZone timeZoneWithName:#"IST"]];
[localDateFrm setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSString* local_string = [localDateFrm stringFromDate:currentDate];
NSLog(#"local_string: %#",local_string);//local_string: 14-10-2016 01:37 PM
NSDateFormatter* local_dateFormatter = [[NSDateFormatter alloc] init] ;
[local_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"IST"]];
[local_dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *date = [local_dateFormatter dateFromString:local_string];
NSLog(#"date : %#",date);//date : 2016-10-14 08:07:00 +0000
at last I need 01:37 PM but I get 08:07:00 +0000.when I convert string into NSDate its not give me expected reply(01:37 PM). so the time is different.
so please help me for this.
Thanks.
NSDate will always return utc or gmt date, if you want it to in your local timezone then you need to convert it to string with stringFromDate method of NSDateFormatter.
So, when you want to display your date with your local timezone just convert it to string and then show. And when you want it to as NSDate you can easily convert string to date with dateFromString method and it will return date in utc or gmt again.
And usually you need to display your date in label or any UI so you must need string, you can't show nsdate on label. So, this is standard approach to deal with NSDate
Update :
You can use this date as localnotification's firedate also. execute below demo,
NSString *str = #"2016-10-14 08:07:00";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(#"date : %#",[df dateFromString:str]);
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = date;
notification.alertTitle = #"title";
NSLog(#"notification : %#",notification);
and check log for notification, it will show firetime as per your local timezone!!
Second thing you can set timezone also for notification like notification.timeZone = ...;
I build app that get date string(date in french time) from a database. after i get the string i convert it to nsdate:
NSString *dateStr = "20/3/2016 21:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm zzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
self.date = [dateFormatter dateFromString:dateStr];
Into self.date i get(if i print the object) :
2016-06-10 21:00:00 +0000
But when i try to convert it to local time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSString *time = [formatter stringFromDate:cellItem.date];
I get a different time: 00:00
Any idea what can be the problem?
The default time zone for an NSDateFormatter is the time zone of the device, and as you didn't set it explicitly in the second formatter, that is what will be used.
Use the first formatter for both jobs or set it explicitly as you did in the first formatter:
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
Also a note on:
Into self.date i get (if i print the object)
If you are using NSLog() or the debugger to view an NSDate object, it will use [NSDate description] to generate the string, which will always use the GMT time zone. This constantly trips people up.
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.
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.