-1

i have NSString with this date format "2016-03-16" and i added following code to get the same date in proper NSDate format but its returning " 2016-03-15 18:30:00 +0000 ". How do do i get same "2016-03-16" in NSDate ?

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *datePart = [dateFormatter stringFromDate:dateFromString];//2016-03-16

    NSDate *dateValue = [dateFormatter dateFromString:datePart];
    NSLog(@"%@----",dateValue); //" 2016-03-15 18:30:00 +0000 
Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • 1
    NSDate represents EXACT dates, which most people refer to as date and time. March 16 is an inexact description of a date & time. The system makes an assumption, figures that you mean 12am on 3/16 for the device, and returns that date and time in GMT. Before you solve the problem with code, make sure you understand exactly what time you want in your app. – danh Mar 18 '16 at 20:23
  • http://stackoverflow.com/questions/17294289/unexpected-value-from-nsdate/17296378#17296378 – Amin Negm-Awad Mar 18 '16 at 20:49

3 Answers3

5

Your local time zone is presumably UTC+5:30. You are specifying a date but not a time, so the time is implied to be midnight. Midnight on the 16th in your local time zone is 18:30 the day before (the 15th) in UTC time, which is why you get "2016-03-15 18:30:00 +0000"

When you log the date with NSLog(@"%@----",dateValue) you are actually invoking [dateValue description], which displays the date using UTC.

You can use NSLog(@"%@----",[dateValue descriptionWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone] locale:nil]) and you will see the date in your current time zone.

Be aware though that the description and associated methods such as descriptionWithCalendarFormat methods are only for debugging, you should use an NSDateFormatter to convert dates to strings. iOS_Binod's answer shows one way you could do this.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
3

You try to print NSDate instance in console. That's reason your code print default format value in console.

You need to get string value from NSDate instance with the help of this method [your_dateformater stringFromDate:dateInstance]

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *datePart = @"2016-03-16";
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(@"string convert into Date is - %@",[dateFormatter stringFromDate:dateValue]);
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
dev_binod
  • 427
  • 2
  • 9
2

The date formatter uses your local time zone by default. The -[NSDate description] method (which is what %@ calls) uses UTC. This is why the strings are different.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • i didnt understand anything – Bangalore Mar 18 '16 at 20:03
  • 1
    Then read and learn. Mostly, learn. Google for UTC. You might also google for GMT which is almost but not quite the same as UTC. You know the world is big and round and has time zones? – gnasher729 Mar 18 '16 at 20:29