- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate {
//設(shè)置源日期時(shí)區(qū)
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//設(shè)置轉(zhuǎn)換后的目標(biāo)日期時(shí)區(qū)
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期與世界標(biāo)準(zhǔn)時(shí)間的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目標(biāo)日期與本地時(shí)區(qū)的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到時(shí)間偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//轉(zhuǎn)為現(xiàn)在時(shí)間
NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
return destinationDateNow;
}
例子演示:我的機(jī)器是北京時(shí)區(qū)東八區(qū)。
//2013-08-03T12:53:51+0800 UTC時(shí)間格式下的北京時(shí)間型凳,可以看到北京時(shí)間= UTC + 8小時(shí)又跛。
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *localDate = [dateFormatter dateFromString:@"2013-08-03T04:56:52+0000"];
//+0000 表示的是當(dāng)前時(shí)間是個(gè)世界時(shí)間吠勘。
NSLog(@"now Time = %@",[selfgetNowDateFromatAnDate:localDate]);
結(jié)果:
2013-08-03 12:57:33.391 xxxx[2547:c07] now Time = 2013-08-03 12:56:52 +0000
以上注意一點(diǎn)嗦董,在轉(zhuǎn)出來后帶的時(shí)間是原參數(shù)anydate的時(shí)區(qū)母谎,因此切不可再用NSDateFormatter 轉(zhuǎn)換。否則會(huì)多增加一個(gè)時(shí)區(qū)的時(shí)間值展懈。應(yīng)該使用如下來提取字符串销睁。
NSString *str = [NSStringstringWithFormat:@"%@", [selfgetNowDateFromatAnDate:localDate]];
NSLog(@"str = %@",str);
注NSDate對(duì)象存放的日期始終是UTC的標(biāo)準(zhǔn)時(shí)間,可以根據(jù)這個(gè)時(shí)間進(jìn)行其它時(shí)間的轉(zhuǎn)換存崖。因此上面算出來的時(shí)間中時(shí)區(qū)為 +0000,如果此時(shí)再轉(zhuǎn)為字符串睡毒。
//將本地日期字符串轉(zhuǎn)為UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定輸入輸出格式
- (NSString *)getUTCFormateLocalDate:(NSString *)localDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//輸入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//輸出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}
//將UTC日期字符串轉(zhuǎn)為本地時(shí)間字符串
//輸入的UTC日期格式2013-08-03T04:53:51+0000
- (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//輸入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localTimeZone];
NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
//輸出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
return dateString;
}
- (NSString *)getUTCFormatDate:(NSDate *)localDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
return dateString;
}
- (NSDate *)getLocalFromUTC:(NSString *)utc {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *ldate = [dateFormatter dateFromString:utc];
return ldate;
}
//以上注意字符串時(shí)的輸入?yún)?shù)的格式来惧,別外不要用%@來查看NSDate的值,
//因?yàn)楸旧泶娴木褪荱TC 演顾,小心被誤倒供搀。將期轉(zhuǎn)換出字符串來查看一下隅居。
下面要說一下iOS-NSDateFormatter格式說明:
G: 公元時(shí)代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月葛虐,顯示為1-12
MMM: 月胎源,顯示為英文月份簡寫,如 Jan
MMMM: 月,顯示為英文月份全稱屿脐,如 Janualy
dd: 日涕蚤,2位數(shù)表示,如02
d: 日的诵,1-2位顯示万栅,如 2
EEE: 簡寫星期幾,如Sun
EEEE: 全寫星期幾西疤,如Sunday
aa: 上下午烦粒,AM/PM
H: 時(shí),24小時(shí)制代赁,0-23
K:時(shí)扰她,12小時(shí)制,0-11
m: 分芭碍,1-2位
mm: 分义黎,2位
s: 秒,1-2位
ss: 秒豁跑,2位
S: 毫秒
Z:GMT
常用的時(shí)間格式有:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy