2015-03-31 58 views
-1

我想知道什麼是NSDateFormatter概念。我在我的應用程序中使用過它,但我對它的格式有點困惑。NSDateFormatter本週的最後一天

  1. 設置在我的模擬器:

    settings->Language & Region United States,Calendar Gregorian calendar. 
    
  2. 我已經使用這個代碼,日期轉換爲字符串:

    NSDateFormatter*defaultFormatter = [[NSDateFormatter alloc] init]; 
    [defaultFormatter setDateFormat:@"MM/dd/yyyy"]; 
    
  3. 要得到周的最後一天,我用這個代碼。這裏date2015-03-31 18:30:00 +0000

    NSDate *today =date;  
    
    //Week End Date 
    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    [gregorianEnd setFirstWeekday:1]; 
    
    NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today]; 
    
    int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week 
    
    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week 
    
    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd]; 
    
    NSString* dateEndPrev = [defaultFormatter stringFromDate:EndOfWeek]; 
    
    NSDate* weekEndPrev = [defaultFormatter dateFromString:dateEndPrev] ; 
    NSLog(@"EndDate:%@",weekEndPrev); //prints ===>2015-04-04 18:30:00 +0000 
    
    return [defaultFormatter stringFromDate:weekEndPrev]; //==>04/05/2015 
    

我不知道爲什麼它從給定的日期將在第二天。

回答

0
-(void)some method 
{ 
NSDate *today = [NSDate date]; 
NSLog(@"Today date is %@",today); 
dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format. 

//Week End Date 
NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today]; 

int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday]; 
// this will give you current day of week 

[componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week 

NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd]; 
NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init]; 
[dateFormat_End setDateFormat:@"yyyy-MM-dd"]; 
dateEndPrev = [dateFormat stringFromDate:EndOfWeek]; 
weekEndPrev = [[dateFormat_End dateFromString:dateEndPrev] retain]; 
NSLog(@"%@",weekEndPrev); 
} 
0

你的代碼看起來有點脆弱,對我來說過於複雜。
根據用戶的地區/日曆,這會給你一週內最後一天的開始。

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDate *now = [NSDate date]; 
NSDate *startOfTheWeek; 
NSDate *endOfWeek; 
NSDate *startLastDayOfWeek; 
NSTimeInterval interval; 
[cal rangeOfUnit:NSCalendarUnitWeek 
     startDate:&startOfTheWeek 
     interval:&interval 
     forDate:now]; 
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1]; // 23:59:59 
[cal rangeOfUnit:NSCalendarUnitDay 
     startDate:&startLastDayOfWeek 
     interval:NULL 
     forDate:endOfWeek]; 
0

有可用的計算一週的最後一天多種方式,有效的方式來獲得一週的最後一天 一個是

的NSDate * weekDate = [NSDate的日期]

NSCalendar * myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents * currentComps = [myCalendar組分:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)FROM日期:weekDate];

int weekOfYearVal = currentComps.weekOfYear;

[currentComps setWeekday:1];
NSDate * firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

[currentComps setWeekday:7]; NSDate * lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];

myDateFormatter.dateFormat = @「dd EEEE」;

NSString * firstDayOfWeek = [myDateFormatter stringFromDate:firstDayOfTheWeek];

NSString * lastDayOfWeek = [myDateFormatter stringFromDate:lastDayOfTheWeek]; NSLog(@「星期的第一天:%@」,firstDayOfWeek); NSLog(@「LastWeek:%@」,lastDayOfWeek);

請讓我知道你是否想要更多的信息。

相關問題