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.
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 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);
I know this is a very question and asked multiple times before.But this is something i get stuck every time.
Now, i want to convert a date string from web service which is in Argentina local zone(UTC-3:0) and want to convert this that to device local time zone(Suppose UTC+5:30). Here is my code
-(NSDate *)getLocaDateStringFromDate:(NSString *)sourceDate andSourceTime:(NSString *)sourceTime
{
//sourceDate is #"2015-10-16"; and sourceTime is #"00:00:00"
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
}
NSString* source = [sourceDate stringByAppendingString:[NSString stringWithFormat:#" %#", sourceTime]];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:source];
NSLog(#"sourceZone time is %#" , [df stringFromDate: ds]);//sourceZone time is 2015-10-16 00:00:00, correct!
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: ds]);//local time is 2015-10-16 08:30:00,, correct
NSString* str = [df stringFromDate: ds];
return [df dateFromString:str]; //this return 2015-10-16 03:00:00 +0000, why?
}
My question is that why my method return date in UTC, regardless of my time zone set to systemTimeZone. I get correct string but date is incorrect.
Any help would be appreciated.
1.) First, create an NSDateFormatter to get the NSDate sent from the server.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
2.) Convert the date string to a NSDate.
NSDate* sourceDate = [dateFormatter dateFromString:dateString];
3) Specify ,Local and Destination timezone in which you want to convert your date.
For getting timezone of your source date:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
For getting User timezone :
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
4.) Calculate the inverval between source timezone and user timezone as given bellow:
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
5.) Now, create an NSDateFormatter for formatting date in which you want to show.
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
Hope it is useful for you :)
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 want to find the time (in minutes) between a date with the "yyyy-MM-dd'T'HH:mm:ss" format (Sample:"2014-02-03T11:28:00") and current time (In GMT, as the string is in GMT. I tried
NSDateFormatter *formatter;
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *now = [NSDate date];
NSDate *trackingDataDate = [formatter dateFromString:timeStamp];
NSTimeInterval distance = [now timeIntervalSinceDate:trackingDataDate];
double timeInMinutes = distance / 60;
But this returns NaN. What is wrong with my code, how can I get the time between the events?
Simple mistake. You have miss allocation for NSDateFormatter. Add below line instead of NSDateFormatter *formatter;, It's working fine.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
Update: According to your comment, see my below code for convert to GMT Date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
Try this:
NSString* string=#"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(#"Date = %#",date);