-1

below is my sample code.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
NSLog(@"Date : %d",dateFormatted);

The output is 2016-01-16 18:30:00 +0000. How to get date Formatt 2016-01-17 . what is the reason for one day delay. please help me.

kb920
  • 3,039
  • 2
  • 33
  • 44
Baskar
  • 33
  • 6
  • 1
    What is your time zone? – Thilo Jan 21 '16 at 11:22
  • Time Zone: india. am date is fixed but result logger print is one day delay. what is the reason. any timezone use but date is constant string values. – Baskar Jan 21 '16 at 11:47
  • 1
    Well, India is +5:30 or so. so Jan, 17, 00:00 for you will be Jan 16, 18:30 in Greenwich. You have to tell the formatter to print in your timezone. – Thilo Jan 21 '16 at 11:51
  • a NSDate is just a moment in time, counting seconds from 1/1/1970. if you print it it will display it's value in UTC. – vikingosegundo Jan 21 '16 at 13:05
  • 1
    Baskar, the very first thing that you should learn is that India is 5 1/2 hours ahead of GMT, so midnight of the 17th in India _is_ 18:30 on the 16th GMT. The result is absolutely correct. – gnasher729 Jan 21 '16 at 13:17

3 Answers3

2

You shouldn't create the NSDate from the NSString without timezone mark.

  1. Add timezone to the NSString like "-0800":

    e.g. "2016-01-17 -0800"  --> "yyyy-MM-dd Z"
    
  2. Add code:

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];// change timezone u want
    
Justlike
  • 1,476
  • 10
  • 18
1
   NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:dateFormatted];
    NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:dateFormatted];

    NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
    NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dateFormatted] ;

    NSLog(@"Date : %@",destinationDate);
Rahul Patel
  • 1,822
  • 12
  • 21
0

You can use below code...!

I am forcing the Date to be 2016-01-17 in first 2 scenarios. U can check and use if it is useful to you..!

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * sourceDate = [dateFormatter dateFromString:@"2016-01-17"];

    int daysToAdd = 1;
    NSDate *presentDate = [sourceDate dateByAddingTimeInterval:60*60*24*daysToAdd]; //Output 2016-01-17 18:30:00 +0000

    NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
    NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components1 = [calendar1 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateFormatted];
    [components1 setHour:24];//24
    [components1 setMinute:00];//00
    NSDate *presentDate1 = [calendar1 dateFromComponents:components1]; //Output 2016-01-17 18:30:00 +0000

    //The input
    NSString *myDateAsAStringValue = @"2016-01-17";

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [df setDateFormat:@"yyyy-MM-dd"];

    //Getting Date
    NSDate *myDate = [df dateFromString: myDateAsAStringValue]; // Output 2016-01-17 00:00:00 +0000

    //create the formatter for the output
    NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
    [out_df setDateFormat:@"yyyy-MM-dd"];

    NSString *dateStr=[out_df stringFromDate:myDate];//2016-01-17

Hope it helps you...!

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59