NSDate is not returning my local Time zone /default time zone of device

后端 未结 7 1967
孤城傲影
孤城傲影 2020-11-27 16:02

My local and default time zone is GMT +5 but when I get date and time by NSDate it return me GMT date and time.

For example the code and output from my code while

相关标签:
7条回答
  • 2020-11-27 16:15

    Try this

    - (NSDate *) GetLocalDate {
    
        NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
        NSCalendar *theCalendar = [NSCalendar currentCalendar];
        theCalendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
        dayComponent = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    
        NSDate *localDate = [theCalendar dateFromComponents:dayComponent];
    
        return localDate;
    }
    
    - (void)viewDidLoad {
            [super viewDidLoad];
            NSLog(@"localDate : %@",[self GetLocalDate]);
    }
    
    0 讨论(0)
  • 2020-11-27 16:15

    If you want local Date and time. Try this code:-

    NSString *localDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
    
    0 讨论(0)
  • 2020-11-27 16:27

    Eliseo Chavez Jr answer translated to Swift

    let sourceDate = NSDate();
    let sourceTimeZone = NSTimeZone(abbreviation: "GMT")!;
    let destinationTimeZone = NSTimeZone.systemTimeZone();
    let sourceGMTOffset: NSInteger  = sourceTimeZone.secondsFromGMTForDate(sourceDate);
    let destinationGMTOffset:NSInteger  = destinationTimeZone.secondsFromGMTForDate(sourceDate);
    let interval: NSTimeInterval = Double (destinationGMTOffset - sourceGMTOffset);
    let destinationDate = NSDate(timeInterval: interval, sinceDate: sourceDate);
    
    0 讨论(0)
  • 2020-11-27 16:30

    NSDate is a "raw" date. That's why it is in GMT. It's up to the code to use NSDateFormatter (as you have done) to output the date to a value that makes sense for the user.

    In some rare cases you might need to display an NSDate not using the users time zone (like if you want to display a time in New York time no matter where the user is). Then, you set the time zone on the date formatter to a specific value (again, as you have done).

    It's common practice for computers to store all dates in GMT, and then adjust how they are displayed for the user. If you start trying to alter how the date is actually stored you are going to mess up a lot of date handling frameworks that are all assuming your NSDate is in GMT.

    As you have seen, when you read in a date via an NSDateFormatter, it's converted from the time entered based on the timezone of the formatter you have set, and then converted to GMT. So what do you think is wrong with what it is doing? Because it's doing just what it should - storing dates in GMT and outputting strings based on a timezone.

    0 讨论(0)
  • 2020-11-27 16:32

    Try this...

    NSDate* sourceDate = [NSDate date];
    
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
    

    This help respond to the current system timezone.

    0 讨论(0)
  • 2020-11-27 16:34

    Forget NSDateFormatter - it's way too complicated, instead try this magic:

    NSLog(@"Date with local timezone is: %@",
    [date descriptionWithLocale:NSLocale.systemLocale]);
    
    0 讨论(0)
提交回复
热议问题