Javascript convert date to seconds

JavaScript Date getSeconds()

getSeconds() returns the seconds (0 to 59) of a date.

Syntax

Parameters

Return Value

Browser Support

getSeconds() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Javascript convert date to seconds

Last updated: Jan 14, 2023
Reading time · 4 min

banner

# Table of Contents

# Convert days to milliseconds in JavaScript

To convert days to milliseconds, multiply the days by 24 for the hours, 60 for the minutes, 60 for the seconds and 1000 for the milliseconds.

Copied!
function daysToMilliseconds(days) // 👇️ hour min sec ms return days * 24 * 60 * 60 * 1000; > console.log(daysToMilliseconds(1)); // 👉️ 86400000 console.log(daysToMilliseconds(2)); // 👉️ 17280000 console.log(daysToMilliseconds(5)); // 👉️ 432000000

We created a reusable function that takes the number of days as a parameter and converts the days to milliseconds.

To convert the days to milliseconds, we have to multiply by 24 to convert to hours, 60 to convert to minutes, 60 to convert to seconds and 1000 to convert to milliseconds.

If your application uses fractional units for the days, you might get a decimal number after the conversion.

Copied!
function daysToMilliseconds(days) // 👇️ hour min sec ms return days * 24 * 60 * 60 * 1000; > console.log(daysToMilliseconds(5.5612)); // 👉️ 442367999.99999

You can use the Math.round() function to round the number to the nearest integer.

Copied!
function daysToMilliseconds(days) // 👇️ hour min sec ms return Math.round(days * 24 * 60 * 60 * 1000); > console.log(daysToMilliseconds(5.5612)); // 👉️ 442368000

We passed the value to the Math.round function to round to the nearest integer and make sure we don’t get a decimal after the conversion.

Here are some examples of how the Math.round function works.

Copied!
console.log(Math.round(3.49)); // 👉️ 3 console.log(Math.round(3.5)); // 👉️ 4

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5 , it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5 , it gets rounded to the lower absolute value.

# Convert days to seconds using JavaScript

To convert days to seconds, multiply the days by 24 for the hours, 60 for the minutes and 60 for the seconds.

The result from the multiplication will represent the seconds equivalent of the number of days.

Copied!
function daysToSeconds(days) // 👇️ hour min sec return days * 24 * 60 * 60; > console.log(daysToSeconds(1)); // 👉️ 86400 console.log(daysToSeconds(2)); // 👉️ 172800 console.log(daysToSeconds(5)); // 👉️ 432000

We created a reusable function that takes the number of days as a parameter and converts the days to seconds.

To convert the days to seconds, we have to multiply by 24 to convert to hours, 60 to convert to minutes and 60 to convert to seconds.

If your application uses fractional units for the days, you might get a decimal number after the conversion.

Copied!
function daysToSeconds(days) // 👇️ hour min sec return days * 24 * 60 * 60; > console.log(daysToSeconds(3.5612)); // 👉️ 307687.68

You can use the Math.round() function to round the number to the nearest integer.

Copied!
function daysToSeconds(days) // 👇️ hour min sec return Math.round(days * 24 * 60 * 60); > console.log(daysToSeconds(3.5612)); // 👉️ 307688

We passed the value to the Math.round function to round to the nearest integer and make sure we don’t get a decimal after the conversion.

Here are some examples of how the Math.round() function works.

Copied!
console.log(Math.round(7.49)); // 👉️ 7 console.log(Math.round(7.5)); // 👉️ 8

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5 , it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5 , it gets rounded to the lower absolute value.

# Convert Days to Minutes using JavaScript

To convert days to minutes, multiply the days by 24 for the hours and 60 for the minutes.

The result from the multiplication will represent the minutes equivalent of the number of days.

Copied!
function daysToMinutes(days) // 👇️ hour min return days * 24 * 60; > console.log(daysToMinutes(1)); // 👉️ 1440 console.log(daysToMinutes(2)); // 👉️ 2880 console.log(daysToMinutes(5)); // 👉️ 7200

We created a reusable function that takes the number of days as a parameter and converts the days to minutes.

To convert the days to minutes, we have to multiply by 24 to convert to hours and 60 to convert to minutes.

If your application uses fractional units for the days, you might get a decimal number after the conversion.

Copied!
function daysToMinutes(days) // 👇️ hour min return days * 24 * 60; > console.log(daysToMinutes(3.43)); // 👉️ 4939.20000001

You can use the Math.round() function to round the number to the nearest integer.

Copied!
function daysToMinutes(days) // 👇️ hour min return Math.round(days * 24 * 60); > console.log(daysToMinutes(3.43)); // 👉️ 4939

We passed the value to the Math.round function to round to the nearest integer and make sure we don’t get a decimal after the conversion.

Here are some examples of how the Math.round function works.

Copied!
console.log(Math.round(2.49)); // 👉️ 2 console.log(Math.round(2.5)); // 👉️ 3

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5 , it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5 , it gets rounded to the lower absolute value.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Читайте также:  Redefinition of unused python
Оцените статью