gpt4 book ai didi

ios - 如何以 24 小时格式获取日期

转载 作者:行者123 更新时间:2023-11-29 01:19:02 25 4
gpt4 key购买 nike

NSDate *localDate = [NSDate date];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:localDate];
NSDate *gmtDate = [localDate dateByAddingTimeInterval:-timeZoneOffset];

这给了我 12 小时格式的时间。因此,例如现在是下午 2.15。它给了我 2016-01-16 02:15:00 +0000。

但是,我想要 24 小时格式,例如 14:15:00。我也想要时间。我怎样才能得到它。

最佳答案

如果您需要一个以您需要的格式表示时间的字符串,请尝试使用 NSDateFormatter 并将其设置为 GMT timeZone 和所需的 dateFormat (HH:mm: ss 在你的情况下)

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSLog(@"Current time in GMT = %@", [formatter stringFromDate:[NSDate date]]);

这将以 GMT 时区格式打印时间,如 12:34:56

有关日期格式的更多信息,请参阅 this fine article

关于ios - 如何以 24 小时格式获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com