1

在我的应用程序中,想要从日期中获取日期(即SundayMonday等)。

我的代码如下:

NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //ask for current week
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:currentDate];
    //create date on week start
    NSDate* weekstart=[calendar dateFromComponents:comps];

    //add 7 days
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
    for (int i=1; i<=7; i++) {
        NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
        compsToAdd.day=i;
        NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
        NSLog(@"%@",nextDate);  // date is 2013-06-30 18:30:00 +0000



        NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
        [myFormatter setDateFormat:@"EEEE"]; // day, like "Saturday"
        //[myFormatter setDateFormat:@"c"]; // day number, like 7 for saturday

        NSString *dayOfWeek = [myFormatter stringFromDate:nextDate];

问题

        NSLog(@"Today is: %@", dayOfWeek);  // Prints Mondays instead of Sunday
    }

我该如何解决这个问题?

4

7 回答 7

6
 NSCalendar* calender = [NSCalendar currentCalendar];

 NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

int weekDay = [component weekday];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekDay - 1];

NSLog(@"Day ::: %@", weekdayString);
于 2014-02-07T08:45:20.763 回答
6

尝试这个 :

    NSCalendar* calender = [NSCalendar currentCalendar];
    NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    return [component weekday]; // 1 for Sunday, 2 for Monday, and so on...

并查看来自 NSDate 的这一天名称? 而这个https://discussions.apple.com/thread/1700102?start=0&tstart=0

编辑

尝试这个

NSString * time = @"2013-06-30T11:00:26+0100";

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date1 = [df dateFromString:time];
long lgTime = (long)[date1 timeIntervalSince1970];
NSLog(@"%ld", lgTime);


NSCalendar* calender = [NSCalendar currentCalendar];
NSDateComponents* component = [calender components:NSWeekdayCalendarUnit fromDate:date1];

NSLog(@"date=%@ === %@",[NSDate date],date1);
NSLog(@"day= %d",[component weekday]); ///it return 1
于 2013-07-01T11:51:33.947 回答
5
NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEEE"];
NSString *weekDay =  [theDateFormatter stringFromDate:[NSDate date]];
于 2013-07-01T11:51:36.510 回答
1

我做了一个小NSDate类别,可以帮助您从日期中找到工作日名称。它返回工作日名称的本地化字符串。看看它。如果你愿意,你可以使用它。

NKLocalizedWeekday+NSDate

基本上,这是您需要的代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekdayNumber = [components weekday];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *weekdayString = [[formatter weekdaySymbols] objectAtIndex:weekdayNumber - 1];
于 2013-07-01T11:59:48.567 回答
0

你面临问题,因为它需要当地时区

尝试这个

  NSDateFormatter *dateformate = [[NSDateFormatter alloc] init] ;
 [dateformate setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];
 [dateformate setDateFormat:@"EEEE"];
  NSLog(@"%@", [dateformate stringFromDate:[NSDate date]]);
  NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);

设置您需要的时区。它可以工作并帮助您。

NSCalendar 每周第一天链接中的类似问题参考它。也可以帮助你。

于 2013-07-01T12:30:43.103 回答
0
mydate = [NSDate date];
NSDateFormatter *dt2 = [ [NSDateFormatter alloc] init];
[dt2 setDateFormat:@"EEEE"];

NSString *day = [[NSString alloc] initWithFormat:@"%@",[dt2 stringFromDate:mydate]];
于 2013-07-01T12:09:33.470 回答
0

我正在使用它,它在 iOS 8.4 中对我有用

-(NSString *) extractDayFromDate {
     NSCalendar* calender = [NSCalendar currentCalendar];
     NSDateComponents* component = [calender components:NSCalendarUnitWeekday fromDate:[NSDate date]];
     return [[calender weekdaySymbols] objectAtIndex:([component weekday]-1)]; 
}
于 2015-09-18T13:38:12.473 回答