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 format. CFAabsolute time is a numeric value representing the number of milliseconds since the Unix epoch whereas a date object is an object representing a specific date and time, year, month, day, hour, minute, second and millisecond with respect to the client browser.

In this tutorial we have learned both −

  • Convert CFAbsoluteTime to Date Object

  • Convert Date Object to CFAbsoluteTime

Convert CFAbsoluteTime to Date Object

CFAbsoluteTime is elapsed time since Jan 1, 2001, 00:00:00 UTC and the date object is elapsed time since Jan 1, 1970, 00:00:00 UTC so we need to add the difference between them to CFAbsoluteTime which is 978307200, also the unit is in milliseconds, so additionally, we need to multiply the result by 1000.

Let's see the step-by-step approach for more clearance.

  • Initialize a CFAbsoluteTime value, which we'll convert to a Date object value.

  • Create a new Date object, pass the initialized value, and add 978307200 to it, then multiply them by 1000 as it is in milliseconds.

  • Finally, print the result.

Example

In this example, we are converting a CFAbsoluteTime to a Date Object using JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Converting CFAbsoluteTime to Date Object in JavaScript</title>
</head>
<body>
   <h2> Convert CFAbsoluteTime to Date Object </h2>
   <p id="input">CFAbsoluteTime: </p>
   <p id="output">Date Object: </p>
   <script>
      // CFAbsoluteTime
      let CFAbsoluteTime = 641347496.743657;
      document.getElementById("input").innerHTML += CFAbsoluteTime ;
      
      // Convert CFAbsoluteTime to Date Object
      let date = new Date((CFAbsoluteTime + 978307200)  * 1000);
      
      // Display the result
      document.getElementById("output").innerHTML += date;
   </script>
</body>
</html>

Convert Date Object to CFAbsoluteTime

To convert the CFAbsoluteTime to Date object we need to do the exact opposite of what we have done before to convert the CFAbsoluteTime to Date object.

Here is the step-wise procedure to convert the CFAbsoluteTime to Date object in JavaScript −

  • Initialize a Date object with the date and time passed as arguments, which we'll convert to CFAbsoluteTime.

  • Then use the getTime() method with the above object and divide it by 1000, remember in the above step we have multiplied 1000, here we are just reversing the process.

  • Then finally subtract 978307200 with the result, just like we add when converting CFAbsoluteTime to Date object.

  • Finally, print the result.

Example

In this example, we are converting the CFAbsoluteTime to Date object using JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Converting Date Object to CFAbsoluteTime JavaScript</title>
</head>
<body>
   <h2> Convert Date Object to CFAbsoluteTime </h2>
   <p id="input">Date Object: </p>
   <p id="output">CFAbsoluteTime: </p>
   <script>
      // Creting date object
      var date = new Date("June 15, 2023 16:16:36");
      document.getElementById("input").innerHTML += date;
      
      // convert date to CFAbsoluteTime
      var CFAbsoluteTime = (date.getTime() / 1000) - 978307200;
      
      // display CFAbsoluteTime
      document.getElementById("output").innerHTML += CFAbsoluteTime;
   </script>
</body>
</html>

Summary

Let's summarize what we've learned in this tutorial. We see that to convert a CFAbsoluteTime to a Date object or a Date object to a CFAbsoluteTime, we just need to perform a simple mathematical operation. We need to see that there are two differences between the two times, the CFAbsoluteTime is the elapsed time since Jan 1, 2001, 00:00:00 UTC and the Date object is the elapsed time since Jan 1, 1970, 00:00:00 UTC. Therefore, the difference between them is 978307200, which we can use to convert from one form to another by addition or subtraction. The second is the unit which is in milliseconds for CFAbsoluteTime, which we can convert by multiplying 1000 and vice-versa.

Updated on: 21-Apr-2023

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements