【问题标题】:Convert utc date into 12 hours local date?将UTC日期转换为12小时本地日期?
【发布时间】:2020-03-25 04:56:44
【问题描述】:

我的日期和时间是 2019 年 11 月 20 日 21:09,采用 UTC 24 小时格式。现在我想将其转换为 12 小时格式的当地时间。 2019 年 11 月 30 日上午 8:00 像这样。

我的代码是:

// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];

当我将本地时间 12 格式发送到 24 小时 UTC 时我的代码

-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
//    NSLog(@"%@", localDate);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"];
    NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormatter.locale = twelveHourLocale;
    NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
    NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
    NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
    NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
//    NSLog(@"dateString %@", dateString);
    return dateString;
}

-(NSDate *)getUTCDate:(NSString *)currentDate{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MMM-yyyy HH:mm"];

    NSDate *date1 = [dateFormat dateFromString:currentDate];
    if (date1 == nil){
         [dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
         date1 = [dateFormat dateFromString:currentDate];
    }
    return date1;
}

【问题讨论】:

标签: ios objective-c cocoa-touch nsdate nsdateformatter


【解决方案1】:

我认为你做得太多了。格式“hh”是 12 小时格式的小时,HH 是 24 小时格式。您不必为此设置区域设置(尽管设置为 en_US_POSIX 确实避免了用户在 [NSLocale currentLocale] 实例中的 24 小时偏好,这可以覆盖 iOS 上的设置)。

NSDate 是时间上的绝对实例。您需要使用 NSDateFormatter 应用日历和时区以从中获取数字年/月/日等值,但您不需要将偏移量调整为参考日期(即更改实际日期,而不是只是在不同的时区重新格式化)。

NSString *utcString = @"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"dd-MMM-yyyy HH:mm";

NSDate *date = [formatter dateFromString:utcString];

formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = @"dd-MMM-yyyy hh:mm a";
NSLog(@"date: %@", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多