34

在我的 iPhone 应用程序中,我很难将 web 服务响应时间转换为当前设备时间。webservice time in UTC-5 timezone。_ 该应用程序是全球使用的应用程序。所以我必须将响应时间转换为当前设备时区。这是一个简单的消息应用程序。

当我发送了一个message the time is "5:05 PM" (India Chennai timezone)但当我webservice is "2012-07-16 07:33:01". 当我在应用程序中显示时间时,我应该将时间转换为设备的本地时间。我已经尝试过某种方式,但我还不能解决。代码是:

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
[dateFormatter1 setLocale:[NSLocale currentLocale]];
//[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"IST"]];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

但是输出日期和时间是2012-07-16 02:03:01 +0000我已经存储了这个日期/时间Coredata,当我从下面的代码中检索CoreData和隐藏时,NSString

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate:dateString];
NSLog(@"DateString : %@", dateStr);

输出日期和时间为“ Today 7:33 AM”。它应该显示“ Today 5:05 PM”。谁能拯救我的一天?请帮助解决这个问题。提前致谢。

4

3 回答 3

78

我已经测试了您的场景并添加了一些代码供您参考。请测试以下内容,请让我知道它对您有用。

NSString *dateStr = @"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(@"DateString : %@", dateStr);

谢谢。

于 2012-07-17T12:38:12.620 回答
10
NSDate* currentDate = [NSDate date];

NSTimeZone* CurrentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* SystemTimeZone = [NSTimeZone systemTimeZone];

NSInteger currentGMTOffset = [CurrentTimeZone secondsFromGMTForDate:currentDate];
NSInteger SystemGMTOffset = [SystemTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = SystemGMTOffset - currentGMTOffset;

NSDate* TodayDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
NSLog(@"Current time zone Today Date : %@", TodayDate);

斯威夫特 4

var currentDate = Date()
var CurrentTimeZone = NSTimeZone(abbreviation: "GMT")
var SystemTimeZone = NSTimeZone.system as NSTimeZone
var currentGMTOffset: Int? = CurrentTimeZone?.secondsFromGMT(for: currentDate)
var SystemGMTOffset: Int = SystemTimeZone.secondsFromGMT(for: currentDate)
var interval = TimeInterval((SystemGMTOffset - currentGMTOffset!))
var TodayDate = Date(timeInterval: interval, since: currentDate)
print("Current time zone Today Date : \(TodayDate)")

希望对其他开发者有所帮助!快乐编码!

于 2016-03-29T11:20:12.190 回答
5

愿这个扩展会更容易。

Swift 4:UTC/GMT ⟺ 本地(当前/系统)

extension Date {

    // Convert local time to UTC (or GMT)
    func toGlobalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

    // Convert UTC (or GMT) to local time
    func toLocalTime() -> Date {
        let timezone = TimeZone.current
        let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
        return Date(timeInterval: seconds, since: self)
    }

}


// Try it
let utcDate = Date().toGlobalTime()
let localDate = utcDate.toLocalTime()

print("utcDate - (utcDate)")
print("localDate - (localDate)")
于 2018-02-19T13:26:39.380 回答