Sunday, October 6, 2024
Google search engine
HomeLanguagesJavascriptHow to Convert CFAbsoluteTime to Date Object and vice-versa in JavaScript ?

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

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 Type unitConversionValue
Seconds 1000
Milliseconds 1
Nanoseconds 0.000001

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

Whether youā€™re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions weā€™ve already empowered, and weā€™re here to do the same for you. Donā€™t miss out ā€“ check it out now!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?