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

好久不见. 提交于 2019-11-26 11:57:00

问题


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.

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:

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 +0000 I have stored this date/time in Coredata and when I have retrieve from CoreData and covert to NSString using the below code,

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.


回答1:


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.




回答2:


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

Hope it helps to another developers ! Happy Coding !




回答3:


May this extension would be easier.

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

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


来源:https://stackoverflow.com/questions/11504843/get-current-iphone-device-timezone-date-and-time-from-utc-5-timezone-date-and-ti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!