How do I convert GMT to IST in IOS ? I have code which will give me time in GMT. But I want to convert it into the corresponding local time zone and use it in my apps. How can I do this ?
Thanks in Advance.
The code below convert GMT to IST.
NSString *inDateStr = #"2000/01/02 03:04:05";
NSString *s = #"yyyy/MM/dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(#"[in]%# -> [out]%#", inDateStr, outDateStr);
[(NSDateFormatter *) setTimeZone:[NSTimeZone localTimeZone]]; this will give u the local conversion of the GMT to local time zone.
I was used below code to convert GMT to IST.
NSTimeZone *zone1=[NSTimeZone localTimeZone];
long second=[zone1 secondsFromGMT]; //offset
NSDate *currentdate=[NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMddyyyy hh:mm:ss"]; // format
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:second]];
dateStr = [dateFormat stringFromDate:currentdate];
NSLog(#"Current date %#",dateStr);
It may be helpful.
Thank you
Related
I have an NSString like this :
NSString *playTime = #"20:45";// GMT
I need to convert this string to NSDate (only hours and seconds) then adjusted to device timezone.
Example:
If user device timezone is set to GMT, I need a result like this : #"20:45";
If user device timezone is set to GMT + 2, I need a result like this : #"22:45";
Try this
NSString *gmtDateString = #"20:45";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"HH:mm"];
//Now you have date in GMT time zone by default dateFormatter timezone would be in local time zone
NSDate *date = [dateFormatter dateFromString:dateString];
//set to local time zone
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//Now you have string in localDate format
NSString *localDateString = [dateFormatter stringFromDate:date];
The below code will calculate the time w.r.t. local time zone.
Suppose my time zone is GMT+05:30 so the time 05:30 will become 00:00.
NSString *playTime = #"20:45";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"HH:mm";
NSDate *date = [formatter dateFromString:playTime];
NSLog(#"%#",date);
Log is: 2000-01-01 15:15:00 +0000
Use this code for date conversion as per your requirement.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"HH:mm"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
logDate = [dateFormat dateFromString:#"20:45"];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);
I need to convert an NSString to an NSDate, but the following code only works as long as the user's device isn't set to 24-Hour time.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *aTime = #"2/11/2013 12:00:00 AM";
NSDate *aDate = [formatter dateFromString:aTime];
However, aDate returns null when the device is in 24-Hour time. Any help?
I think you're probably seeing a side effect of the behaviour described by QA1480. Apple has resolved what it presumably thought was developers not obeying locales properly by modifying your prescribed date format unless you explicitly say not to.
You can achieve that by adding:
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
/* and autorelease if not using ARC */
Which basically says 'the date format I just set is explicitly what I want, please don't modify it in any way'.
I think the following snippet might helps you out.
NSString *dateString = #"09:34 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd-MM-yyyy";
NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
dateFormatter.dateFormat = #"dd-MM-yyyy hh:mm a";
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDate *returnDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# %#", stringFromDate, dateString]];
try this code
NSString *string = #"2015-11-30 15:00:00";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [formatter dateFromString:string];
date: 2015-11-29 21:30:00 +0000
its in UTC format you can modify this as you need!
I am trying to convert an NSString to an NSDate with time. Here's what I'm doing now:
NSString *myDateIs= #"2012-07-14 11:30:40 AM ";
NSDateFormatter* startTimeFormat = [[NSDateFormatter alloc] init];
[startTimeFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss a "];
NSDate*newStartTime = [startTimeFormat dateFromString:myDateIs];
The output is 2012-07-14 06:00:40 +0000. The date is correct but the time is not correct.
How can I get the correct time?
Thanks.
The time you are getting is in GMT convert it into local time.
NSDateFormatter* local = [[[NSDateFormatter alloc] init] autorelease];
[local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[local setDateFormat:#"YYYY-MM-dd hh:mm:ss a"];
NSString* localSTR = [local stringFromDate: newStartTime];
Time Zone. The NSDate will store the time in terms of GMT. However your local time zone is probably quite different.
You need to set the timezone.
NSString *myDateIs= #"2012-07-14 11:30:40 AM ";
NSDateFormatter* startTimeFormat = [[NSDateFormatter alloc] init];
startTimeFormat.timeZone=[NSTimeZone timeZoneWithName:#"UTC"];
[startTimeFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss a"];
NSDate*newStartTime = [startTimeFormat dateFromString:myDateIs];
NSLog(#"%#", newStartTime);
Output:
2012-07-14 11:30:40 +0000
This one will work for you:
[startTimeFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss aa"];
[startTimeFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
You missed an a at the dateFormat and have to set the timeZone.
So I have the following code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSLog(#"FIRST USED %# %f", dateString, [[NSDate date] timeIntervalSinceDate:self.firstUsed]);
FIRST USED 06/15/2012 10:42PM 716.087895
The thing that confuse me is that when I put in 173.755023 it doesn't translate back to June 15th 2012 10:42. How is this possible? I think the correct number should be 1339714440
Try this :
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
int offset = [timeZone secondsFromGMTForDate: referenceDate];
int unix_timestamp = [curdate timeIntervalSince1970];
int Timestamp = unix_timestamp - offset;
Please write down Following Code :
NSDateFormatter *dtFormatAppointment = [[NSDateFormatter alloc] init];
[dtFormatAppointment setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSTimeInterval time = [dateString timeIntervalSince1970];
NSLog(#"FIRST USED %# %.f", dateString, time);
this code is working for you.
I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code