2013-01-11 84 views
0

在我的應用程序中,我必須以表格視圖的日期顯示工作時間列表,如何使用當前日期(今天)在iPhone中使用當前日期(今天)獲取本週的所有日期/日期

在這裏,我有5個場景

  1. 今天的項目,工時
  2. 選定日期的項目,小時
  3. Cureent周(週日至週六)
  4. 本月(一月工作時間,如果二月這是行軍二月如果是行軍)
  5. 本年度(所有工作時間2013)

我實施的SQLite查詢如下,

1.選擇日期,ProjectTitle,WorkingHours,總和從ProjDetailsTable其中date = toDaysDate

2.選擇日期,ProjectTitle,WorkingHours,從ProjDetailsTable總和(WorkingHours)其中日期= selecteDate

(WorkingHours)

I have done for the fist two scenarions

爲3,4,5的情況


3. Select Date, ProjectTitle, WorkingHours, sum(WorkingHours) from ProjDetailsTable where 
Date between two Dates 

但我的問題是,如果我有日期

Is there any logic to get the starting for 
current week, current month, current year 

注意:不是7天前,不是前30個月,也不是365天后。

我的要求是:

if Current date is Feb-13-2020 WednesDay

本週:月9至今天

本月:月1至今天

今年:揚1-2020到今天

回答

3

我希望這會給你你怎麼也得進行基本思想: 對於其他情況下,您可以制定出

- (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]; 
    } 
+0

非常感謝yu,它的工作很好嗎有什麼辦法可以得到本月的第一天和第一天的第一天 – user1811427

0

你可以使用這個獲得當前年份&個月。 只需將這些添加到我的朋友建議的以前的答案。

NSInteger year = [comps year]; 
NSInteger month = [comps month]; 

現在,我認爲,如果你要本年度&月,你可以手動設置爲第一個日期。並按照你的意願表演。

重要的是得到工作日.. 謝謝。

相關問題