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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11504843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:15:42  来源:igfitidea点击:

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

iosobjective-cswiftnsdatensdateformatter

提问by Gopinath

In my iPhone application i am very struggling to convert the webservice response time to current device time. The webservice time in UTC-5 timezone. The app is worldwide use app. So i have to convert the response time to current device timezone. This is simple messaging app.

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

When i have sent a message the time is "5:05 PM" (India Chennai timezone)but when am retrieve time from webservice is "2012-07-16 07:33:01". When i show the time in the app i should convert the time to device's local time. I have tried in some way but i can't solve yet. Code is:

当我发送了一个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);

But the output date and time is 2012-07-16 02:03:01 +0000I have stored this date/time in Coredataand when I have retrieve from CoreDataand covert to NSStringusing the below code,

但是输出日期和时间是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);

The output date and time is "Today 7:33 AM". It should be show "Today 5:05 PM". Can anyone please save my day? Please help to solve this issue. Thanks in advance.

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

回答by Yuvaraj.M

I have tested your scenario and added some code for your reference. Please test the below and please let me know it is useful for you.

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

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

Thanks.

谢谢。

回答by Mahesh Cheliya

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

斯威夫特 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)")

Hope it helps to another developers ! Happy Coding !

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

回答by Krunal

May this extension would be easier.

可能这个扩展会更容易。

Swift 4: UTC/GMT ? Local (Current/System)

斯威夫特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)")