在iOS中将本地时间转换为EST

时间:2016-01-29 06:09:44

标签: ios nsdateformatter nslocale

请建议如何将当地时间转换为EST时间。 我用google搜索了但是没有得到任何相关的答案。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

svn commit -m "Deleting file"

答案 1 :(得分:2)

-(NSString * )convertTimeZoneFromDateString:(NSString *)string
{
 NSDateFormatter * format = [[NSDateFormatter alloc] init];

// from server dateFormat
[format setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss '+0000'"];

// get date from server
NSDate * dateTemp = [format dateFromString:string];

// new date format
[format setDateFormat:@"MMM dd, hh:mm a"];

// convert Timezone
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];

NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:dateTemp];

NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:dateTemp];

NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

// get final date in LocalTimeZone
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dateTemp];

// convert to String
NSString *dateStr = [format stringFromDate:destinationDate];

return dateStr;
}

答案 2 :(得分:0)

Swift 2.0:

let todayDateString: String = "2016-12-17 06:36:25"
let gmtDateformat: NSDateFormatter = NSDateFormatter()

gmtDateformat.timeZone = NSTimeZone(name: "GMT")
gmtDateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
let gmtDate: NSDate = gmtDateformat.dateFromString(todayDateString)!
let estDateformat: NSDateFormatter = NSDateFormatter()
estDateformat.timeZone = NSTimeZone(name: "EST")
estDateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
let estDate: NSDate = estDateformat.dateFromString(gmtDateformat.stringFromDate(gmtDate))!

print("EST Time \(estDate)")