ios NSDateFormatter转换日期结果错误

发布于 2025-01-21 07:47:53 字数 406 浏览 1 评论 0原文

我的代码是fllow:

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = "yyyy-MM-dd";
    fmt.timeZone = NSTimeZone.systemTimeZone;
    NSString *selfStr = [fmt stringFromDate:self];
    NSDate *date = [fmt dateFromString:selfStr];

如果我设置self = @“ 2022-04-13”,日期将是“ 2022-04-13 10:00:00 +0000” 然后,我将日期转换为时间戳将不是“ 2022-04-13 00:00:00”

我不知道为什么?

my code is as fllow:

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = "yyyy-MM-dd";
    fmt.timeZone = NSTimeZone.systemTimeZone;
    NSString *selfStr = [fmt stringFromDate:self];
    NSDate *date = [fmt dateFromString:selfStr];

if i set self = @"2022-04-13",the date will be "2022-04-13 10:00:00 +0000"
then i transform date to timestamp will be not "2022-04-13 00:00:00"

i can't know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2025-01-28 07:47:53

删除当前日期的时间部分的更好方法是

NSCalendar *cal = NSCalendar.currentCalendar;
NSDate *date = [cal startOfDayForDate:NSDate.date];
NSLog(@"%@", date);

考虑到日期字符串2022-04-13 10:00:00 +0000显示了UTC时区。这与2022-04-13 00:00:00 -1000是同一时间点

A better way to drop the time portion of the current date is

NSCalendar *cal = NSCalendar.currentCalendar;
NSDate *date = [cal startOfDayForDate:NSDate.date];
NSLog(@"%@", date);

Consider that the date string 2022-04-13 10:00:00 +0000 shows the UTC time zone. It's the same point in time as 2022-04-13 00:00:00 -1000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文