我有一个NSDate
对象,我发送给我们使用的API。我们的应用程序在全球范围内使用,因此API希望日期为UTC,例如:
"/Date(1481704200000)/"
但是,日期需要考虑时区偏移,因为API /服务器没有为我们这样做。如果我们不这样做,当我们在服务器上查看它时,日期是不正确的。
我正在执行以下操作,这似乎产生了错误的日期:
// Offset
float timezoneoffset = [[NSTimeZone localTimeZone] secondsFromGMT];
// Date
CFTimeInterval startDate = [[NSDate date] timeIntervalSince1970];
startDate = startDate - timezoneoffset;
NSString *dateStarted = [NSString stringWithFormat:@"/Date(%.0f000)/", startDate];
如何正确考虑本地时区并创建格式正确的UTC日期字符串?
答案 0 :(得分:5)
无需任何时区操作。
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.activity_menubar_toolbar);
setSupportActionBar(toolbar);
autoCompleteTextView = (AutoCompleteTextView)toolbar.findViewById(R.id.autoCompleteTextView);
无论用户的时区如何,都以UTC为单位提供时间。
试试吧。在测试设备上使用几个不同的时区设置运行此代码,您将在每种情况下获得相同的结果。
答案 1 :(得分:1)
double secsUtc1970 = [[NSDate date]timeIntervalSince1970];
现在是UTC,(至少在使用下面的方法之后) 要将此时间存储为UTC(自参考日期1970起),请使用
NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];
// or Timezone with specific name like
// [NSTimeZone timeZoneWithName:@"Europe/Riga"]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];
设置日期格式化程序以输出本地时间:
NSDate
NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"Name : %@", timeZoneName);
对象始终使用UTC作为时间参考,但日期的字符串表示不一定基于UTC时区。
要获得时区,您可以使用:
[NSTimeZone abbreviationDictionary]
如果您致电NSLog(@"%@",[NSTimeZone abbreviationDictionary]);
,我们会收到包含所有可用缩写词的词典。
2015-07-10 11:44:05.954 APP[1472:26120] {
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
}
结果:
{{1}}
希望这会有所帮助。
答案 2 :(得分:-1)
public let Date_format_Default:String = "yyyy-MM-dd HH:mm:ss"
为UTC字符串设置此格式字符串;
public let Date_format_Z:String = "yyyy-MM-dd HH:mm:ssZ"
public let Time_zone_UTC:String = "UTC"
open class func convertDate2UTC(_ date:String)->(String){
var df_:DateFormatter?=DateFormatter()
df_!.dateFormat=Date_format_Z
let tz_=TimeZone(identifier: Time_zone_UTC)
df_!.timeZone=tz_
let df_s=df_!.date(from: date)
df_!.dateFormat=Date_format_Default
let string_=df_!.string(from: df_s!)
df_ = nil
return string_;
}