如何使用iPhone中的当前日期(今天)获取今年,本月,本周的所有日期/日期 [英] How to get all the days/dates in current week, this month, this year using the current date (today) in iPhone
问题描述
在我的应用程序中,我必须在表格视图中按日期显示工作时间列表,
In my application i have to show list of working hours by date in a table view,
这里我有5个场景
- 今天的项目,工作时间
- 选定的日期项目,小时
- Cureent周(周日 - 星期六)
- 当月(如果是3月份,如果是3月份,则是2月的工作时间)
- 当年(所有工作时间) 2013)
- Today's projects, working hours
- Selected date projects, hours
- Cureent week (sunday-saturday)
- Current month (working hours in january, February if it is feb march if it is march)
- Current year (all working hours in 2013)
我已经实现了如下sqlite查询,
i have implemented sqlite queries as follows,
1。从ProjDetailsTable中选择日期,ProjectTitle,WorkingHours,sum(WorkingHours),其中date = toDaysDate
2。从ProjDetailsTable中选择日期,ProjectTitle,WorkingHours,sum(WorkingHours),其中date = selecteDate
我已经为拳头两个场景
对于3,4,5场景
For the 3,4,5 scenarios
3. Select Date, ProjectTitle, WorkingHours, sum(WorkingHours) from ProjDetailsTable where
Date between two Dates
但我的问题是如果我有日期
But My problem is if I have the dates
Is there any logic to get the starting for
current week, current month, current year
注意:不是7天回来,而不是30个月,而不是365天。
Note : not the 7 days back, not the 30 months back and not the 365 days back.
我的要求是:
如果当前日期是2月13-2020 WednesDay
if Current date is Feb-13-2020 WednesDay
本周: 2月9日至今
本月: 2月1日至今日
今年: Jan-1-2020到今天
推荐答案
我希望这能让您了解如何继续前进:
对于其他情况,您可以解决这个问题
I hope this will give you basic idea how you have to proceed: For other scenarios you can work out
- (NSDate *)firstDayOfWeekFromDate:(NSDate *)date {
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
[comps setWeekday:2]; // 2: monday
return [calendar dateFromComponents:comps];
}
这篇关于如何使用iPhone中的当前日期(今天)获取今年,本月,本周的所有日期/日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!