简体   繁体   English

本地化NSDate并转换NSDate的时区

[英]Localize the NSDate and Convert timezone of NSDate

In my iPhone / iPad app I am showing a UIDatePicker for time. 在我的iPhone / iPad应用程序中,我正在显示UIDatePicker。 It will display time in this format, 11:00 AM. 它将以这种格式显示时间11:00 AM。 When User clicks on the time row we expand the time row to display this datePicker row. 当用户单击时间行时,我们将展开时间行以显示此datePicker行。 I have a time stamp in string "starts": "11:00", // time of day in ISO-8601 format I need to show this on the picker wheel as selected time when it gets opened up. 我在字符串“ starts”中有一个时间戳:“ 11:00”,// ISO-8601格式的一天中的时间,我需要在拾纸轮上将其显示为打开时的选定时间。 For this, first of all I get the date at 12 AM using https://stackoverflow.com/a/9040554/4082792 . 为此,首先我使用https://stackoverflow.com/a/9040554/4082792来获取12点的日期。 Then I convert the given time (11:00) to number of seconds and add it to the midnight time to get the current time. 然后,我将给定时间(11:00)转换为秒数,并将其添加到午夜时间以获取当前时间。 This time is in local timezone (as I specify the timezone while using NSDateFormatter). 该时间位于本地时区(因为我在使用NSDateFormatter时指定了时区)。 When I try to setDate to UIDatePicker with this date, It gives me incorrect time, even though the time is correct in the NSDate variable. 当我尝试使用该日期将setDate设置为UIDatePicker时,即使NSDate变量中的时间正确,它也会给我不正确的时间。 For 11:00 AM, it gives me 6:40 while the local time is 4:30. 对于11:00 AM,当地时间是4:30,所以给我6:40。 So, I have two questions : 1) Why is the time wrong on wheel. 因此,我有两个问题:1)为什么时间错了? 2) How can I convert the NSDate from one timezone to another, I need to show it in the local time format. 2)如何将NSDate从一个时区转换为另一个时区,我需要以本地时间格式显示它。

Snippet : 片段:

NSString *strDate = @"11:00";
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
        NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
        date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:date]];


        ///Start time
        NSString *startTime = @"11:00";
        NSArray *startTimeSeparatedByColon = [startTime componentsSeparatedByString:@":"]; /// From 22:10 to [22, 10];
        NSInteger hourPartOfStart = startTimeSeparatedByColon[0] ? [startTimeSeparatedByColon[0] integerValue] : 0;
        NSInteger minutePartOfStart = startTimeSeparatedByColon[1] ? [startTimeSeparatedByColon[1] integerValue] : 0;
        NSTimeInterval totalTime = (hourPartOfStart*60*60+minutePartOfStart*60);

        NSDate *finalDate =   [NSDate dateWithTimeInterval:totalTime sinceDate:date];

        NSDate *dt = [NSDate dateWithTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT sinceDate:finalDate];
        self.datePicker.date = dt;

By default, iOS converts date into the device's time zone. 默认情况下,iOS会将日期转换为设备的时区。 But if you want to convert date into another time zone, here is the code for that: 但是,如果要将日期转换为另一个时区,请使用以下代码:

NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:currentDateTimeZone];

You can get the date in "EST" time zone from this dateFormatter object. 您可以从此dateFormatter对象获取“ EST”时区中的日期。

Convert "EDT" TimeZone 转换“ EDT”时

 NSString *format = @"EEEE dd-MMMM-yyyy HH:mm:ss z";
 NSDateFormatter *date_EDTDateFormate = [[NSDateFormatter alloc] init];
 date_EDTDateFormate setDateFormat:format];
 date_EDTDateFormate setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]];
 NSString *stringEDT = [dateFormatter_EDT stringFromDate:date_System];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM