gpt4 book ai didi

iphone - 获取 NSDate 今天、昨天、本周、上周、本月、上个月...变量

转载 作者:行者123 更新时间:2023-12-03 18:08:37 26 4
gpt4 key购买 nike

我想做的是让 NSDate 今天、昨天、本周、上周、本月、上个月变量准备好进行比较,以便在 UITableView 的 titleForHeaderInSection 上添加标题

我想要的是在下面的代码中手动完成日期 2009-12-11

 NSDate *today = [NSDate dateWithString:@"2009-12-11 00:00:00 +0000"];
NSDate *yesterday = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
NSDate *thisWeek = [NSDate dateWithString:@"2009-12-06 00:00:00 +0000"];
NSDate *lastWeek = [NSDate dateWithString:@"2009-11-30 00:00:00 +0000"];
NSDate *thisMonth = [NSDate dateWithString:@"2009-12-01 00:00:00 +0000"];
NSDate *lastMonth = [NSDate dateWithString:@"2009-11-01 00:00:00 +0000"];

最佳答案

改编自Date and Time Programming Guide :

// Right now, you can remove the seconds into the day if you want
NSDate *today = [NSDate date];

// All intervals taken from Google
NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0];
NSDate *thisWeek = [today dateByAddingTimeInterval: -604800.0];
NSDate *lastWeek = [today dateByAddingTimeInterval: -1209600.0];

// To get the correct number of seconds in each month use NSCalendar
NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83];
NSDate *lastMonth = [today dateByAddingTimeInterval: -5259487.66];

如果您想要根据月份获得正确的确切天数,则应使用 NSCalendar .

关于iphone - 获取 NSDate 今天、昨天、本周、上周、本月、上个月...变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1889164/

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