How to Convert CFAbsoluteTime to Date Object and vice-versa in JavaScript ? Last Updated : 23 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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. JavaScript const CFAbsoluteTimeToDate = (CFATime, unitConversionValue = 1000) => { const dt = new Date('January 1 2001 GMT'); dt.setTime(dt.getTime() + CFATime * unitConversionValue); return dt; }; console.log(CFAbsoluteTimeToDate(639494700)); Output: 2021-04-07T13:25:00.000Z 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. JavaScript const DateToCFAbsoluteTime = (dt, unitConversionValue = 1000) => { const CFADate = new Date('January 1 2001 GMT'); // unitConversionValue; return (dt.getTime() - CFADate.getTime()); }; console.log(DateToCFAbsoluteTime( 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 TypeunitConversionValueSeconds1000Milliseconds1Nanoseconds0.000001 By default, the program will use seconds, which is the most common CFAbsoluteTime storage type. Comment More infoAdvertise with us Next Article How to Convert CFAbsoluteTime to Date Object and vice-versa in JavaScript ? xtrp Follow Improve Article Tags : JavaScript Web Technologies javascript-date JavaScript-Questions Similar Reads How to Convert Date to Another Timezone in JavaScript? Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries.1. Using Intl.DateTimeFormat() and format( 2 min read How to convert a JavaScript date to UTC? Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By defa 1 min read How to calculate the yesterday's date in JavaScript ? In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that 3 min read How to get Day Month and Year from Date Object in JavaScript ? We have given the Date object and the task is to extract the Day, Month, and Year from the Date object and print it using JavaScript. Below is an example for better understanding. Example:Input: Date ObjectOutput: Day: 26 Month: 11 Year: 2023To extract Day, Month, and Year from Date Object, we have 3 min read How to convert UTC date time into local date time using JavaScript ? Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. html 1 min read How to Log the Current Date in JavaScript ? The Date object is an inbuilt object constructor of JavaScript language. It is used to work with dates and times. The Date object is created by using the new keyword, i.e. new Date(). The current date can be logged on the console in JavaScript using the below-discussed methods. Table of Content Usin 3 min read How to get JavaScript Date in UTC using MomentJS? When working with dates and times in JavaScript, especially in applications that deal with users across different time zones, it is often necessary to work with dates in UTC (Coordinated Universal Time). Below are the approaches to get a date in UTC using Moment.js:Table of ContentUsing moment().utc 2 min read How to convert JavaScript datetime to MySQL datetime ? To convert a JavaScript Date object to a MySQL datetime format, you can use the toISOString method to get the date in ISO format, and then manipulate the string to fit the MySQL datetime format. Given a date in JavaScript DateTime format the task is to convert this time into MySQL DateTime format us 1 min read How to Format a Date in JavaScript? Here are different ways to format the date in JavaScript.1. Using the toDateString() MethodThe toDateString() method formats the date object into a human-readable format as Day Month Date Year.SyntaxdateObj.toDateString();JavaScriptconst date = new Date(); const formatDate = date.toDateString(); con 2 min read How to Get the Current Date in JavaScript ? The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string.SyntaxdateObj.toDateString()JavaScript// Create a Date Obj 2 min read Like