Home > Web Front-end > JS Tutorial > body text

[Daily Package] ms

PHPz
Release: 2024-09-01 21:13:06
Original
239 people have browsed it

[Daily Package] ms

ms

ms converts various time formats to milliseconds and vice versa.

/* time format to milliseconds */
ms('2 days')  // 172800000
ms('1d')      // 86400000
ms('10h')     // 36000000
ms('2.5 hrs') // 9000000
ms('2h')      // 7200000
ms('1m')      // 60000

/* milliseconds to time format */
ms(60000)             // "1m"
ms(2 * 60000)         // "2m"
ms(-3 * 60000)        // "-3m"
ms(ms('10 hours'))    // "10h"
Copy after login

Why do we need this?

It's okay to use milliseconds directly in your code.

setTimeout(() => {
  console.log('Hi')
}, 180_000)
Copy after login

But this might not be a good practice, because it's hard to tell how many minutes 180,000 milliseconds is at a glance.

You can improve readability by commenting or using constants.

const THREE_MINUTES_IN_MS = 180_000

setTimeout(() => {
  console.log('Hi')
}, THREE_MINUTES_IN_MS)
Copy after login

I actually used to write in this way, and if you're not handling milliseconds a lot, this is a better option than ms.

However, if you're not, ms is a good choice. You don't have to write an extra variable, and it's much easier to change the time. ?

The above is the detailed content of [Daily Package] ms. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!