先說下自己開發(fā)過程中之前迷惑的兩點(diǎn):
1 unix 時(shí)間戳使用 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #3495af}span.s1 {font-variant-ligatures: no-common-ligatures}
NSDateFormatter 進(jìn)行轉(zhuǎn)換時(shí)载荔,會(huì)自動(dòng)轉(zhuǎn)換為當(dāng)前時(shí)區(qū)的時(shí)間咽袜。
2 字符串的類型的時(shí)間球化,如“2016-01-01”傲醉,系統(tǒng)會(huì)默認(rèn)為是當(dāng)前時(shí)區(qū)的時(shí)間茬暇,轉(zhuǎn)換為NSDate時(shí)是0時(shí)區(qū)的 旋恼。
自已定義的轉(zhuǎn)換方法
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #008f00}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #3495af}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; min-height: 14.0px}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'PingFang SC'; color: #008f00}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 12.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #3495af}span.s4 {font-variant-ligatures: no-common-ligatures; color: #0433ff}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #b4261a}span.s7 {font: 12.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font: 12.0px Menlo; font-variant-ligatures: no-common-ligatures}
// serverTime 為標(biāo)準(zhǔn)時(shí)間戳
- (NSString *)dateStringWithServerTime:(double)serverTime
{
return [self dateStringWithServerTime:serverTime withForm:@"yyyy-MM-dd HH:mm:ss"];
}
//
-
(NSString *)dateStringWithServerTime:(double)serverTime withForm:(NSString *)form
{
// date 是0時(shí)區(qū)的時(shí)間
NSDate *date = [NSDate dateWithTimeIntervalSince1970:serverTime];// 系統(tǒng)會(huì)默認(rèn)轉(zhuǎn)化為東八區(qū)時(shí)間
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:form];
NSString *dateStr = [dateFormatter stringFromDate:date];
return dateStr;
}
// serverTime 為標(biāo)準(zhǔn)時(shí)間戳
-
(NSTimeInterval)secondsWithDateString:(NSString *)dateString withForm:(NSString *)form
{
//系統(tǒng)會(huì)認(rèn)為字符串是東八區(qū)的時(shí)間, 轉(zhuǎn)乘NSDate是零時(shí)區(qū)的NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:form];
NSDate *date = [dateFormatter dateFromString:dateString];
// //將轉(zhuǎn)換回來的對(duì)象手動(dòng)加上8小時(shí)未舟,回到北京時(shí)間
// NSDate *date2 = [date dateByAddingTimeInterval:8 * 60 * 60];
// // 添加默認(rèn)系統(tǒng)時(shí)差 為當(dāng)前時(shí)間
// NSTimeInterval interVal = [[NSTimeZone defaultTimeZone] secondsFromGMTForDate:[NSDate date]];
// NSDate *date3 = [date dateByAddingTimeInterval:interVal];
return [date timeIntervalSince1970];
}
//
- (NSTimeInterval)secondsWithDateString:(NSString *)dataString
{
return [self secondsWithDateString:dataString withForm:@"yyyy-MM-dd HH:mm:ss"];
}
// 獲取 當(dāng)前0時(shí)區(qū)的日期 serverTime 為標(biāo)準(zhǔn)時(shí)間戳
- (NSDate *)dateWithSeconds:(double)serverTime
{
return [NSDate dateWithTimeIntervalSince1970:serverTime];
}