1

I have used following code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSString *str = [dateFormatter stringFromDate:[NSDate date]];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSDate *dateToday =  [dateFormatter dateFromString:str];

Here value for str is correct with .milliseconds but when I try to convert this same string with same used datFormatter in NSDate, it returns NSdate without milliseconds.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • `NSTimeInterval timeInSeconds = [[NSDate date] timeIntervalSince1970];` – Nirav Kotecha Apr 25 '18 at 10:03
  • OR `NSTimeInterval timeInSeconds = [[NSDate date] timeIntervalSince1970] * 1000;` these two can not helps you...? – Nirav Kotecha Apr 25 '18 at 10:04
  • @NiravKotecha I want Date - 2018-04-25T15:47:38.693. in this format, Here .693 is milliseconds value. dateFromString is ignoring this & returning - 2018-04-25T15:47:38 – user8154740 Apr 25 '18 at 10:18
  • Converting NSTimeInterval also returning - 2049-04-25 10:24:05 +0000. i.e. without milliseconds – user8154740 Apr 25 '18 at 10:25
  • are you printing the description of NSDate? – tryKuldeepTanwar Apr 25 '18 at 10:32
  • How do you know converting back from the string representation loses the factional seconds? You don't show how you are testing this. Could it be that they way you are *viewing* the final date is omitting the fractional seconds? – CRD Apr 25 '18 at 11:08
  • NSLog the 'dateToday' value – user8154740 Apr 25 '18 at 13:16
  • Instead of using `NSLog` to check try comparing the before & after values using `NSDate` values, the `timeIntervalSince...` values, or by converting your final `NSDate` value to a string again and comparing the two strings. Do you still lose the fractional seconds? – CRD Apr 25 '18 at 17:09

1 Answers1

1

Use these functions and try to log the dates to get the accurate results :-

-(NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone localTimeZone];
    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    NSString *str = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"Date : %@",str);
    return str;
}

-(NSDate *)dateFromString:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone localTimeZone];
    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    NSDate *date=[dateFormatter dateFromString:dateString];
    NSLog(@"Date : %@",[dateFormatter stringFromDate:date]);
    return date;
}
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • @ dreamBegin in your method - (NSDate *)dateFromString: , NSLog(@"Date : %@",[dateFormatter stringFromDate:date]); prints correct string with milliseconds but NSDate *date=[dateFormatter dateFromString:dateString]; does not, I don't want string with milliseconds , I want is NSDate with milliseconds – user8154740 Apr 25 '18 at 10:55
  • how do you know that your date doesn't have milliseconds? – tryKuldeepTanwar Apr 25 '18 at 11:39
  • NSLog the NSDate – user8154740 Apr 25 '18 at 13:14