Skip to content
Related Articles

Related Articles

How to Convert CFAbsoluteTime to Date Object and vice-versa in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Apr, 2021
View Discussion
Improve Article
Save Article

CFAbsolute time is a standard time format on Apple devices and programming languages like Swift. It stores the amount of nanoseconds since January 1, 2001. At its core, it is similar to epoch time, except it stores times closer to the modern day, using 2001 as a reference point rather than 1970. Here is Appleā€™s official documentation for CFAbsolute time.

To convert CFAbsoluteTime to Date object and vice-versa, we’ll mainly using the JavaScript Date.getTime() method, which provides the amount of milliseconds since January 1, 1970. We’ll also use the Date.setTime() method, that sets the amount of milliseconds since January 1, 1970.

Converting From CFAbsoluteTime to a Date Object

To convert from CFAbsolute Time to a normal date, we can create a Date object from January 1, 2001, and simply add the CFAbsolute time value (in milliseconds) to the Date.getTime() value.


const CFAbsoluteTimeToDate = (CFATime,
    unitConversionValue = 1000) => {
    const dt = new Date('January 1 2001 GMT');
    dt.setTime(dt.getTime() + CFATime * unitConversionValue);
    return dt;



Converting From a Date Object to CFAbsoluteTime

For converting back to CFAbsolute time, we can simply subtract the Date.getTime() values of the date and January 1, 2001, and then convert that value from milliseconds to nanoseconds.


const DateToCFAbsoluteTime = (dt,
    unitConversionValue = 1000) => {
    const CFADate = new Date('January 1 2001 GMT');
    // unitConversionValue;
    return (dt.getTime() - CFADate.getTime());
    new Date("April 5 2021")));

Working With CFAbsoluteTime in Milliseconds or Nanoseconds

Some versions of CFAbsoluteTime are stored in milliseconds or nanoseconds instead of seconds. To work with these types, simply change the value of the unitConversionValue argument as follows:

Storage TypeunitConversionValue

By default, the program will use seconds, which is the most common CFAbsoluteTime storage type.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!