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

前端 未结 3 1196
梦谈多话
梦谈多话 2020-11-30 20:20

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

3条回答
  •  暖寄归人
    2020-11-30 20:39

    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 !

提交回复
热议问题