Reputation: 695
I am getting a date from coredata and trying to get the weekday of that date.I tried the following code but its not working.
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);
It is working if i am using [NSDate date]
instead of [results valueForKey:@"date"]
.
Upvotes: 0
Views: 184
Reputation: 808
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
NSLog(@"weekday = %d",[todayComponents weekday]);
Upvotes: 0
Reputation: 13600
// 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);
Upvotes: 0
Reputation: 1061
You have to use the same date format which you received from [results valueforKey:@"date"]. Thereafter you try to format the date as how you need.
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];
Upvotes: 0
Reputation: 6718
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.
Upvotes: 0
Reputation: 1528
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];
Upvotes: 3
Reputation: 25144
Try using a NSCalendar
to extract NSDateComponents
from your NSDate
.
Upvotes: 1