从UTC-5时区日期和时间iPhone应用程序获取当前iPhone设备时区的日期和时间?

时间:2012-07-16 12:59:00

标签: ios objective-c swift nsdate nsdateformatter

在我的iPhone应用程序中,我很难将Web服务响应时间转换为当前设备时间。 webservice time in UTC-5 timezonemessage 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”。应该显示“{{1}}”。有人可以保存我的一天吗?请帮忙解决这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:77)

我测试了您的方案并添加了一些代码供您参考。请测试以下内容,请告诉我它对您有用。

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);

感谢。

答案 1 :(得分:8)

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);

Swift 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)")

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

答案 2 :(得分:4)

愿这个扩展更容易。

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)")