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

CFAbsoluteTime is the elapsed time since Jan 1, 2001, 00:00:00 UTC. This is a standard time format on Apple devices. On the other hand, a date object is a built-in object in JavaScript used to represent date and time values. It has many methods for providing formatting and converting date & time from one form to another.

The main difference between CFAbsolute Time and JavaScript Date objects is their reference epoch. CFAbsoluteTime counts seconds since January 1, 2001, whereas JavaScript Date objects count milliseconds since January 1, 1970 (Unix epoch).

In this tutorial we will learn how to:

  • Convert CFAbsoluteTime to Date Object

  • Convert Date Object to CFAbsoluteTime

Understanding the Time Difference

The key to conversion is understanding the epoch difference:

  • CFAbsoluteTime epoch: January 1, 2001, 00:00:00 UTC

  • JavaScript Date epoch: January 1, 1970, 00:00:00 UTC

  • Difference: 978,307,200 seconds (31 years)

Convert CFAbsoluteTime to Date Object

To convert CFAbsoluteTime to a JavaScript Date object, we need to adjust for the epoch difference and convert seconds to milliseconds.

Step-by-step approach:

  • Add 978307200 seconds to adjust from CFAbsoluteTime epoch to Unix epoch

  • Multiply by 1000 to convert seconds to milliseconds

  • Create a new Date object with the result

Example

In this example, we convert a CFAbsoluteTime value to a JavaScript Date object.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Converting CFAbsoluteTime to Date Object</title>
</head>
<body>
   <h2>Convert CFAbsoluteTime to Date Object</h2>
   <p id="input">CFAbsoluteTime: </p>
   <p id="output">Date Object: </p>
   <script>
      // CFAbsoluteTime value (seconds since Jan 1, 2001)
      let CFAbsoluteTime = 641347496.743657;
      document.getElementById("input").innerHTML += CFAbsoluteTime;
      
      // Convert CFAbsoluteTime to Date Object
      // Add epoch difference (978307200) and convert to milliseconds (*1000)
      let date = new Date((CFAbsoluteTime + 978307200) * 1000);
      
      // Display the result
      document.getElementById("output").innerHTML += date;
   </script>
</body>
</html>
CFAbsoluteTime: 641347496.743657
Date Object: Thu Jun 15 2023 16:16:36 GMT+0000 (UTC)

Convert Date Object to CFAbsoluteTime

To convert a JavaScript Date object to CFAbsoluteTime, we reverse the process by subtracting the epoch difference and converting milliseconds to seconds.

Step-by-step approach:

  • Get milliseconds from Date object using getTime()

  • Divide by 1000 to convert milliseconds to seconds

  • Subtract 978307200 to adjust from Unix epoch to CFAbsoluteTime epoch

Example

In this example, we convert a JavaScript Date object to CFAbsoluteTime.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Converting Date Object to CFAbsoluteTime</title>
</head>
<body>
   <h2>Convert Date Object to CFAbsoluteTime</h2>
   <p id="input">Date Object: </p>
   <p id="output">CFAbsoluteTime: </p>
   <script>
      // Create date object
      var date = new Date("June 15, 2023 16:16:36");
      document.getElementById("input").innerHTML += date;
      
      // Convert date to CFAbsoluteTime
      // Get milliseconds, convert to seconds, subtract epoch difference
      var CFAbsoluteTime = (date.getTime() / 1000) - 978307200;
      
      // Display CFAbsoluteTime
      document.getElementById("output").innerHTML += CFAbsoluteTime;
   </script>
</body>
</html>
Date Object: Thu Jun 15 2023 16:16:36 GMT+0000 (UTC)
CFAbsoluteTime: 641347496.743657

Conversion Formula Summary

Conversion Formula Purpose
CFAbsoluteTime ? Date new Date((cfTime + 978307200) * 1000) Add epoch difference, convert to milliseconds
Date ? CFAbsoluteTime (date.getTime() / 1000) - 978307200 Convert to seconds, subtract epoch difference

Conclusion

Converting between CFAbsoluteTime and JavaScript Date objects requires adjusting for the 978,307,200-second epoch difference and handling the seconds/milliseconds unit conversion. These conversions are essential when working with Apple device timestamps in web applications.

Updated on: 2026-03-15T23:19:01+05:30

552 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements