我需要NSString 中的日期轉換為這種格式的:yyyy-MM-dd HH:mm:ss zzz。用了下面代碼:
-(NSString *)setDateStringFormat:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
//[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *dateFormatted = [formatter dateFromString:date];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
return [formatter stringFromDate:dateFormatted];
}
然後輸入日期:27/03/2012
輸出日期: 2012-03-27 00:00:00GMT
輸出的是GMT日期,但是我想要的是UTC日期。請幫忙看看。謝謝
給你提供個程序:
-(NSString *)getUTCFormateDate:(NSString *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}