0

我正在从 coredata 获取日期并尝试获取该日期的工作日。我尝试了以下代码,但它不起作用。

    NSDateFormatter *weekDayFormatter = [[NSDateFormatter alloc]init];
    [weekDayFormatter setDateFormat:@"EEEE"];
    NSDate *weekDay = [results valueForKey:@"date"];
    NSLog(@"%@",weekDay);//This log is working.
    NSString *weekDayString = [weekDayFormatter stringFromDate:weekDay];
    NSLog(@"%@",weekDayString); //This log is displaying null.
    NSLog(@"%@ is spent on %@",[results valueForKey:@"amount"],weekDayString);

如果我使用[NSDate date]而不是[results valueForKey:@"date"].

4

6 回答 6

3
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dateOfInterest];

NSInteger weekday = [weekdayComponents weekday];
// weekday 1 = Sunday for Gregorian calendar

[gregorian release];
于 2013-03-13T11:14:32.113 回答
1

尝试使用 aNSCalendarNSDateComponents您的NSDate.

于 2013-03-13T11:12:39.690 回答
0

I think [results valueForKey:@"date"] gives NSString type object. Check it once. If it is true convert it to NSDate.

Case 1:

 NSDateFormatter *weekDayFormatter = [[NSDateFormatter alloc]init];
    [weekDayFormatter setDateFormat:@"EEEE"];
    NSDate *weekDay = @"2013-03-13 11:06:55 +0000";//here warning is there...
    NSLog(@"%@",weekDay);//This log is working.
    NSString *weekDayString = [weekDayFormatter stringFromDate:weekDay];
    NSLog(@"%@",weekDayString); //It gives NULL...

In this case, answer is null.

Case 2:

NSDateFormatter *weekDayFormatter = [[NSDateFormatter alloc]init];
    [weekDayFormatter setDateFormat:@"EEEE"];
    NSDate *weekDay = [NSDate date];
    NSLog(@"%@",weekDay);//This log is working.
    NSString *weekDayString = [weekDayFormatter stringFromDate:weekDay];
    NSLog(@"%@",weekDayString); //It gives results...

In this case, answer is yourResult.

于 2013-03-13T11:19:09.647 回答
0

您必须使用从 [results valueforKey:@"date"] 收到的相同日期格式。此后,您尝试根据需要格式化日期。

NSDateFormatter *weekDayFormatter = [[NSDateFormatter alloc]init];
[weekDayFormatter setDateFormat:SAME FORMAT YOU RECEIVING];
NSDate *weekDay = [weekDayFormatter dateFromString:[results valueForKey@"date"]];   
NSLog(@"%@",weekDay);
[weekDayFormatter setDateFormat:@"EEEE"];
NSString *weekDayString = [weekDayFormatter stringFromDate:weekDay];
于 2013-03-13T11:19:33.443 回答
0
// Your date as a string
NSString *yourDate = @"02-09-2011 20:54:18";    //  [results valueForKey:@"date"]

// Prepare an NSDateFormatter to convert to and from the string representation
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// set format as your Date string (Eg. dd/MM/yyyy for 25/12/2012)
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

// Parse the string representation of the date
NSDate *date = [dateFormatter dateFromString:yourDate];

NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEEE"];
NSString *weekDay =  [theDateFormatter stringFromDate:date];
NSLog(@"%@",weekDay);
于 2013-03-13T11:26:13.843 回答
0
NSDate *today = [NSDate date];

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

NSDateComponents *todayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];

NSLog(@"weekday = %d",[todayComponents weekday]);
于 2013-03-13T12:32:09.790 回答