gpt4 book ai didi

ios - future 相对日期人类可读形式

转载 作者:行者123 更新时间:2023-11-28 22:41:31 24 4
gpt4 key购买 nike

如何输出人类可读的日期,例如将 NSDATE 转换为人类可读的形式,例如明天、从现在起一周、从现在起 1 个月等?

例如,如果当前时间是 2012 年 1 月 19 日早上 7 点

输入日期为2012年1月20日上午9点

那么该函数应该在明天上午 9 点结束。

我找到了如何在线使用过去的日期而不是 future 的日期来执行此操作。

另外网上找的功能也不具体。任何帮助,将不胜感激!谢谢

最佳答案

您需要使用 NSDateFormatter。来自docs :

setDoesRelativeDateFormatting: Specifies whether the receiver uses phrases such as “today” and “tomorrow” for the date component.

- (void)setDoesRelativeDateFormatting:(BOOL)b

Parameters b YES to specify that the receiver should use relative date formatting, otherwise NO. Discussion If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas, for dates in the future, English may only allow “tomorrow,” French may allow “the day after the day after tomorrow,” as illustrated in the following example.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
[dateFormatter setLocale:frLocale];

[dateFormatter setDoesRelativeDateFormatting:YES];

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];

NSLog(@"dateString: %@", dateString);
// Output
// dateString: après-après-demain

关于ios - future 相对日期人类可读形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14409853/

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