1

I have following code to convert string to date. Is it possible that it sets time as "00:00:00" and not the current time?

NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"yyyy-MM-dd"];
NSString *str = @"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];

This gives dt as "2014-08-08 15:20:00 +0000" because I did the operation at 15:20.

Edit: I am using this date to convert it to integer later to store it in database:

int t = [dt timeIntervalSince1970];
Anuj
  • 1,160
  • 2
  • 20
  • 40
  • What sort of database? CoreData? If so you don't need to convert. Either way, this conversion will be fine. Make sure you use `NSInteger` though. – Fogmeister Oct 08 '14 at 12:21
  • 1
    This entire question is moot. What you are doing is correct. In order to get the string "2014-08-08 15:20:00 +0000" you must be displaying it somewhere. This is making you think there is an error. There isn't. Welcome to working with NSDate. Dealing with TimeZones is something you have to do. – Fogmeister Oct 08 '14 at 12:23
  • sqlite using FCModel wrapper. Why to use NSInteger and not int? – Anuj Oct 08 '14 at 12:24
  • 1
    NSInteger will use the correct 32/64 bit version depending on the device. int is a 32 bit int on either device. Same with CGFloat instead of float. etc... Anyway, just use the date that you have. It is correct. It is time zone differences that are making you think that it is not correct. – Fogmeister Oct 08 '14 at 12:25
  • possible duplicate of [How do I NSLog an NSDate?](http://stackoverflow.com/questions/18521340/how-do-i-nslog-an-nsdate) – Daniel Oct 08 '14 at 12:25
  • Ok. I got it now. Accepting the answer. – Anuj Oct 08 '14 at 12:26

2 Answers2

1

If you are displaying the date dt with NSLog you will see what the date description method provides. If you want to see the date in a specific way that suits you use NSDateFormatter to format the date.

Example:

NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"yyyy-MM-dd"];
NSString *str = @"2014-08-08";
NSDate *dt = [dateformat dateFromString:str];

NSDateFormatter *displayDateformat = [[NSDateFormatter alloc] init];
[displayDateformat setDateFormat:@"yyyy-MM-dd"];
NSString *displayDateString = [displayDateformat stringFromDate:dt];
NSLog(@"displayDateString: %@", displayDateString);

Output:

2014-08-08

Note per Apple docs: "This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT."

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Oh wait. I see now. I though you were the asker. This won't compensate for the time being set as the current time though. It will stop any strings showing the time though. – Fogmeister Oct 08 '14 at 12:06
  • you do not need a second `NSDateFormatter`, you can just reuse `dateformat` since the format is the same in `dateformat` and `displayDateformat` – Daniel Oct 08 '14 at 12:12
  • OK, yeah, apart from duplicate two identical date formatters which isn't necessary this is correct. It is a display issue. – Fogmeister Oct 08 '14 at 12:18
  • I have edited the question. I am not using the date for NSLog but to convert it to integer to store in db. – Anuj Oct 08 '14 at 12:21
1

A good practice is to use NSDateComponents

NSDate *yourDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:yourDate];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
// Set the time components manually
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];            

yourDate = [calendar dateFromComponents:dateComponents];

Update

iOS8 :

[[NSCalendar currentCalendar] startOfDayForDate:[NSDate date]];
Adi Pop
  • 226
  • 2
  • 10
  • @Zaph this is also correct but gets a +1 for using Date Components which is what you should be doing. – Fogmeister Oct 08 '14 at 12:04
  • The point is that the display formatting is not addressed. While it may be better it produces the same result as the OP code. – zaph Oct 08 '14 at 12:11
  • @Zaph The question is about `Is it possible that it sets time as "00:00:00" and not the current time` and not about formatting the output – Daniel Oct 08 '14 at 12:14
  • +1 for `startOfDayForDate`. There are lot's of additions to `NSCalendar` for iOS8. – zaph Oct 08 '14 at 12:34