我从核心数据中获取日期并尝试获取该日期的工作日。我尝试了以下代码,但它无效。
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"]
。
答案 0 :(得分: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];
答案 1 :(得分:1)
尝试使用NSCalendar
从NSDateComponents
中提取NSDate
。
答案 2 :(得分:0)
我认为[results valueForKey:@"date"]
给出了NSString类型的对象。检查一次。如果是真的将其转换为NSDate。
案例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...
在这种情况下,答案为空。
案例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...
在这种情况下,答案是yourResult。
答案 3 :(得分: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];
答案 4 :(得分: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);
答案 5 :(得分:0)
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
NSLog(@"weekday = %d",[todayComponents weekday]);