NSDateFormatter and Time Zone issue?(NSDateFormatter 和时区问题?)
问题描述
我有一个带有 GMT 时间的字符串,我想根据系统时区制作它,但它不能正常工作 -
I have a string with time in GMT and i want to make it according to the system time zone but its not working properly -
NSLog(@"Time Str = %@",Time);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy hh:mm a"];
[NSTimeZone resetSystemTimeZone];
NSLog(@"system time zone = %@",[NSTimeZone systemTimeZone]);
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormat dateFromString:Time];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"date from string = %@",date);
NSLog(@"string from date = %@",[dateFormat stringFromDate:date]);
控制台输出 -
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
时间 Str = 09-12-2011 07:57 AM
Time Str = 09-12-2011 07:57 AM
系统时区 = 亚洲/加尔各答 (IST) 偏移量 19800
system time zone = Asia/Calcutta (IST) offset 19800
字符串中的日期 = 2011-12-09 02:27:00 +0000
date from string = 2011-12-09 02:27:00 +0000
日期字符串 = 09-12-2011 07:57 AM
string from date = 09-12-2011 07:57 AM
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
加尔各答距格林威治标准时间 +5:30,因此日期时间应为 1:27,但显示为 02:27.此外,当我从该日期获取一个字符串时,它向我显示了我用来制作日期的相同字符串,我希望根据系统时区更新字符串.
Calcutta is +5:30 from GMT, so the time in date should be 1:27 but its showing 02:27. Also when i take a string from this date its showing me the same sting that i used to make date, i want the string updated according system time zone.
谢谢
推荐答案
如果日期字符串是 GMT,则不能使用系统时区从 NSString 创建 NSDate.
If the date string is in GMT you can't use your system Timezone to create the NSDate from the NSString.
替换第一次出现的[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
with [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
这篇关于NSDateFormatter 和时区问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NSDateFormatter 和时区问题?


基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01