0

I have some simple code which should convert an NSDate which I retrieve from CoreData. For some reason it is not working in this class when the same code works in other views. What am I doing wrong? Below is the offending code and a screen shot of my log...

NSFetchRequest *request26 = [[NSFetchRequest alloc] init];
[request26 setEntity:entityDiscription];
[request26 setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression26 = [NSExpression expressionForKeyPath:@"date"];
NSExpression *swimLast750Expression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression26]];
NSExpressionDescription *expressionDescription26 = [[NSExpressionDescription alloc] init];
[expressionDescription26 setName:@"swimLast750"];
[expressionDescription26 setExpression:swimLast750Expression];
[expressionDescription26 setExpressionResultType:NSInteger16AttributeType];
[request26 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription26]];
NSPredicate *pred26 = [NSPredicate predicateWithFormat:@"(date >= %@) AND (sport like %@) AND (sessiontype like %@)", swimSinceDateAsDate, sportTypeSwim, sessType1];
[request26 setPredicate:pred26];

NSError *error26;
NSArray *objects26 = [context executeFetchRequest:request26 error:&error26];
if (objects26 == nil) {
    NSLog(@"The fetch request returned an array == nil");
} else {
    NSLog(@"Contents of Array Object26 is:%@", objects26);
    NSDate *sessDate = [[objects26 objectAtIndex:0] valueForKey:@"swimLast750"];
    NSLog(@"NSDate is: %@", sessDate);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MMM-yyyy"];
    NSLog(@"DATE FORMATTER: %@", [formatter stringFromDate:[[objects26 objectAtIndex:0] valueForKey:@"swimLast750"]]);
    NSString *dateString = [formatter stringFromDate:sessDate];
    //NSString *dateString = [formatter stringFromDate:[[objects26 objectAtIndex:0] valueForKey:@"swimLast750"]];
    NSLog(@"dateString is: %@", dateString);
    sw750Last = dateString;
}

NSLog Output

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Sean
  • 440
  • 1
  • 6
  • 20
  • 5
    `sessDate` is not an `NSDate`. Looks like it's maybe a timestamp -- a number of seconds offset from some reference time; could be stored as either an `NSNumber` or `NSString`. – jscs Oct 25 '13 at 05:52

3 Answers3

4

You have set

[expressionDescription26 setExpressionResultType:NSInteger16AttributeType];

which causes the fetch request to return the date as an NSNumber. This number is the number of seconds since the "reference date" 1 January 2001, GMT), so you could convert that to NSDate with

NSNumber *sessDateAsNumber = [[objects26 objectAtIndex:0] valueForKey:@"swimLast750"];
NSDate *sessDate = [NSDate dateWithTimeIntervalSinceReferenceDate:[sessDateAsNumber doubleValue]];

But the better solution is probably to use

[expressionDescription26 setExpressionResultType:NSDateAttributeType];

in the expression description. Then the date is returned as NSDate object, and stringFromDate works as expected.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • you absolutely nailed it. I looked at that code so many times and didn't even clock the attributeType - so annoyed with myself. Thank you for the response all working perfectly now that I've changed it to NSDateAttributeType! Genius! – Sean Oct 25 '13 at 16:45
1

You have to convert the timestamps into date using the below code.I think you will get it as NSNumber so you have to convert it into a double value before

NSTimeInterval interval = [sessDate doubleValue];
NSDate *swimLast750=[NSDate dateWithTimeIntervalSince1970:interval];
vignesh kumar
  • 2,310
  • 2
  • 25
  • 39
  • This assumes that the timestamp is relative to the Unix epoch. – jscs Oct 25 '13 at 05:55
  • This actually does not work. The date is stored as an NSDate but I was retrieving it from coredata as an number so when I converted from an interval since 1970 as you suggested I got a date of 05-Oct-1984 instead of 05-Oct-2013... But thanks for the suggestion... – Sean Oct 25 '13 at 16:53
0

First thing you need to change the time interval to date and then set the specified format. Try below code:-

NSDate *date=[NSDate dateWithTimeIntervalSinceReferenceDate:402663864];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSLog(@"%@",date);
NSString *dateString=[formatter stringFromDate:date];
NSLog(@"dateString=%@",dateString);
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56