I got current GMT time - 2013-05-15 08:55 AM
我现在是格林尼治时间- 2013-05-15 08:55
my current local time is - 2013-05-15 02:06 PM and time zone is - Asia/Kolkata
我现在的本地时间是- 2013-05-15 02:06 PM和时区是- Asia/Kolkata。
I tried to convert the above GMT to my local time with this code
我试图用这个代码将上面的GMT转换为本地时间
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
// timeStamp - is the above GMT time
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"local time is %@",dateFromString);
but this give me wron time as - 2013-05-14 19:04:00 +0000 instead of 2013-05-14 02:06 PM
但是这给了我时间- 2013-05-14 19:04:00 +0000而不是2013-05-14 02:06
Why this happens? please help me to clear this.Thanks in advance
为什么发生这种情况呢?请帮我清理一下。谢谢提前
5 个解决方案
#1
7
Nothing is going wrong, a NSDate
instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000
timezone in the date description).
没有什么出错,NSDate实例永远不会有时区(当然会,但它总是GMT,日期描述中+0000时区表示)。
What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata
but you want it to be GMT
.
您在代码中告诉dateformatter的是您希望解析的日期的时区是Asia/Kolkata,但您希望它是GMT常量。
First you need to make a NSDate
object from the input date string with the time zone set to the correct time zone, GMT
as you indicated.
首先,需要从输入日期字符串中创建一个NSDate对象,将时区设置为正确的时区,如您所示的GMT。
NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"Input date as GMT: %@",dateFromString);
Then when you present the date as an string use :
然后当你用字符串表示日期时:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(@"Date formated with local time zone: %@",dateRepresentation);
#2
5
The shortest way that I found to convert GMT time to your local timezone is
我发现将格林尼治时间转换为当地时区的最短方法是
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];
Furthermore, to convert this date value into string
此外,将这个日期值转换为字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];//use the formatted as per your requirement.
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
#3
0
NSLog(@"local time is %@",[dateFormatter stringFromDate: dateFromString]);
#4
0
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];
NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];
NSDate *date = [gmtDateFormatter dateFromString:timestamp];
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];
NSLog(@"local time is %@",[localDateFormatter stringFromDate:date]);
#5
0
Use timeZoneWithName
使用timeZoneWithName
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Kolkata"]];
[timeFormat setDateFormat:@"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];
#1
7
Nothing is going wrong, a NSDate
instance will never have a timezone ( well it will but it is always GMT, indicated by the +0000
timezone in the date description).
没有什么出错,NSDate实例永远不会有时区(当然会,但它总是GMT,日期描述中+0000时区表示)。
What you are telling dateformatter in your code is that the timezone of the date you want to parse is Asia/Kolkata
but you want it to be GMT
.
您在代码中告诉dateformatter的是您希望解析的日期的时区是Asia/Kolkata,但您希望它是GMT常量。
First you need to make a NSDate
object from the input date string with the time zone set to the correct time zone, GMT
as you indicated.
首先,需要从输入日期字符串中创建一个NSDate对象,将时区设置为正确的时区,如您所示的GMT。
NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"Input date as GMT: %@",dateFromString);
Then when you present the date as an string use :
然后当你用字符串表示日期时:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
NSLog(@"Date formated with local time zone: %@",dateRepresentation);
#2
5
The shortest way that I found to convert GMT time to your local timezone is
我发现将格林尼治时间转换为当地时区的最短方法是
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:dateInGMT];
Furthermore, to convert this date value into string
此外,将这个日期值转换为字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];//use the formatted as per your requirement.
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *localTimeDateString = [formatter stringFromDate:localDateTime];
#3
0
NSLog(@"local time is %@",[dateFormatter stringFromDate: dateFromString]);
#4
0
NSTimeZone* gmtTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Kolkata"];
NSDateFormatter *gmtDateFormatter = [[NSDateFormatter alloc] init];
[gmtDateFormatter setTimeZone:gmtTimeZone];
NSDate *date = [gmtDateFormatter dateFromString:timestamp];
NSDateFormatter *localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:localTimeZone];
NSLog(@"local time is %@",[localDateFormatter stringFromDate:date]);
#5
0
Use timeZoneWithName
使用timeZoneWithName
NSDate *date=[[NSDate alloc]init];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Kolkata"]];
[timeFormat setDateFormat:@"HH:mm"];
NSString *dateStr=[timeFormat stringFromDate:date];