How to Get Nsdate with Millisecond Accuracy

How to get nsdate with millisecond accuracy?

You'll need to use the below method to convert sec. into millisecond:

([NSDate timeIntervalSinceReferenceDate] * 1000)

In Objective-C how to get NSDate with milliseconds? dateFromString: method is ignoring milliseconds

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;
}

How to convert NSDate to milliseconds in Objective-C?

There is a similar post here and here.

Basically you have get the second form the reference date (1 January 2001, GMT) and multiply it with 1000.

NSTimeInterval seconds = [NSDate timeIntervalSinceReferenceDate];
double milliseconds = seconds*1000;

Cheers!

NSDateFormatter milliseconds bug

It seems that NSDateFormatter works only with millisecond resolution, for the
following reasons:

  • By setting a breakpoint in CFDateFormatterCreateDateFromString, one can
    see that this function is called from dateFromString::

    (lldb) bt
    * thread #1: tid = 0x26d03f, 0x018f47d0 CoreFoundation`CFDateFormatterCreateDateFromString, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1
    frame #0: 0x018f47d0 CoreFoundation`CFDateFormatterCreateDateFromString
    frame #1: 0x0116e0ea Foundation`getObjectValue + 248
    frame #2: 0x0116dfc7 Foundation`-[NSDateFormatter getObjectValue:forString:errorDescription:] + 206
    frame #3: 0x0116879f Foundation`-[NSDateFormatter dateFromString:] + 71
    * frame #4: 0x00002d56 foo`main(argc=1, argv=0xbfffee54) + 182 at main.mm:25
  • CFDateFormatterCreateDateFromString() is from
    CFDateFormatter.c
    which is open source. One can see that all calendrical calculations are made using the
    ICU Calendar Classes.

  • It is stated in calendar.h that Calendar uses UDate which has a millisecond resolution:

    /**
    * <code>Calendar</code> is an abstract base class for converting between
    * a <code>UDate</code> object and a set of integer fields such as
    * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, <code>HOUR</code>,
    * and so on. (A <code>UDate</code> object represents a specific instant in
    * time with millisecond precision. See UDate
    * for information about the <code>UDate</code> class.)
    * ...

Date to milliseconds and back to date in Swift

I don't understand why you're doing anything with strings...

extension Date {
var millisecondsSince1970:Int64 {
Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}

init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}

Date().millisecondsSince1970 // 1476889390939
Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC)


Related Topics



Leave a reply



Submit