gpt4 book ai didi

iphone - 在 iphone 中用 “today” 和 “yesterday” 字符串替换日期对象

转载 作者:太空狗 更新时间:2023-10-30 03:21:11 24 4
gpt4 key购买 nike

我想返回带有字符串“today”和“yesterday”的日期对象以及 Objective C 中的日期。欢迎所有评论:

我有格式为@"yyyy-MM-dd HH:mm:ss"] 的日期,然后判断日期是今天还是昨天,如果是,则返回“(Yesterday | Today | Date ) "格式化字符串。

最佳答案

NSDateFormatter 可以做到这一点。然而,这不适用于自定义日期格式,但在大多数情况下,当您需要相对日期时,您会将它们呈现给用户和 you should not use hard coded date formats首先。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterMediumStyle;
df.dateStyle = NSDateFormatterShortStyle;
df.doesRelativeDateFormatting = YES; // this enables relative dates like yesterday, today, tomorrow...

NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-48*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate date]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:24*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:48*60*60]]);

这将打印:

2013-06-06 09:13:22.844 x 2[11732:c07] 6/4/13, 9:13:22 AM
2013-06-06 09:13:22.845 x 2[11732:c07] Yesterday, 9:13:22 AM
2013-06-06 09:13:22.845 x 2[11732:c07] Today, 9:13:22 AM
2013-06-06 09:13:22.846 x 2[11732:c07] Tomorrow, 9:13:22 AM
2013-06-06 09:13:22.846 x 2[11732:c07] 6/8/13, 9:13:22 AM

在具有德语语言环境的设备上,这将打印“Vorgestern”(前天)和“Übermorgen”(后天)作为第一个和最后一个日期。

关于iphone - 在 iphone 中用 “today” 和 “yesterday” 字符串替换日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955406/

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