gpt4 book ai didi

ios - 不考虑时间比较 NSDates

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:11 24 4
gpt4 key购买 nike

<分区>

本质上,我有一个每日提示功能,具有与日期相关的特定提示,我需要一种方法来比较当前的 NSDate 与提示的保存日期,而不考虑时间。显然,1 月 1 日的小费与上午 10 点和 10:30 相关,因此我无法从字面上比较 NSDates。以下是我尝试过的一些方法:

尝试 1 - 不起作用

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
// [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

尝试 2 - 不起作用

NSString * zoneString = [[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] substringFromIndex:([[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] length] - 5)];
NSTimeInterval timeInterval = [[zoneString substringToIndex:3] intValue] * 3600;
timeInterval += [[zoneString substringFromIndex:3] intValue] * 60;

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]];
[formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss ZZZ"];
NSLog(@"%@", [formatter stringFromDate:myDate]);

尝试 3 - 不起作用

NSDate *date = [NSDate date];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&date interval:NULL forDate:date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

if ([[dateFormat stringFromDate:date] isEqualToString:[dateFormat stringFromDate:tipDate]])

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