By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Formatting Time Using The Date Object and String Padding
JavaScript

Formatting Time Using The Date Object and String Padding

Admin
Last updated: 2023/01/25 at 5:31 AM
Admin
Share
4 Min Read
Formatting Time Using The Date Object and String Padding

In this article, we are going to discuss about Formatting Time Using the Date Object and String Padding. So we will see to format the time with the help of Date object, and we are going to use milliseconds, with which we can get time in the format of (0000:00:00.00)[hhhh:mm:ss.ms]. Here we are using milliseconds to get time because we can divide milliseconds with 1000 to get seconds and with seconds we can easily get minutes and hours with little math.

Contents
Formatting Time Using The Date Object and String PaddingYou may also like:

So what we are aiming to get the time and put in the time format with the help of string padding.

Formatting Time Using The Date Object and String Padding

Okay, now let’s jump into the code, here we have created a function named formattedTime(m) with parameter ‘m’. In here we have declared some variables to store time, hours, minutes, seconds, milliseconds. Then we have created an object for Date(), and we have used setTime() method on it which takes milliseconds as parameter.

Now we have divided millisecond with, 3600000 to get hours, This expression we added in Math.floor(), so that we can get hours without decimals (for example, Math.floor(2.5977643)= 2). And we have converted this number to string using toString() and finally we used padStart() method to turn this hour in formatted time hour (for example, padStart(4,”0″) = 0000, padStart(2,”2″) = 22). padStart() method will create a number of string and a string to replace with, like here we have padStart(4, “0”) which will form 4 strings, and blank strings will be replaced with 0s.

Now, to get minutes, we will use formatTime.getMinutes() method, and we will just convert to string and here we need 2 string, so we used padStart(2,"0"). For seconds, we will do the same things as minutes, but here we will use formatTime.getSeconds().

Then for milliseconds, we will use formatTime.getMilliSeconds() to get milliseconds, but we just need milliseconds in 2 digits, for that, we will divide this with 10, and we have to put this expression in Math.round() method to get round of values. (For example, Math.round(formatTime.getMilliseconds(2863)/10) will return 29 milliseconds). And then we just convert into string then again we applied padStart(2,"0") to get result in 2 string.

Lastly, we added time in the targeted format in hms variable and we are just returning it.

let formattedTime = function(m){
     let hms= "",
         formatTime = new Date(),
         hours = "0",
         minutes = "0",
         seconds = "0",
         milli ="0";
            
     formatTime.setTime(m);

     hours = Math.floor(m / 3600000).toString().padStart(4,"0");
     minutes = formatTime.getMinutes().toString().padStart(2,"0");
     seconds = formatTime.getSeconds().toString().padStart(2,"0");
     milli = Math.round(formatTime.getMilliseconds()/       10).toString().padStart(2,"0");
            
     hms = `${hours}:${minutes}:${seconds}.${milli}`;
     return hms;
}
Formatting Time Using The Date Object and String Padding

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Formatting Time Using The Date Object and String Padding, javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Searching an Array For a Value In JavaScript, Searching an Array For a Value
Next Article Create a Non-duplicated Collection JavaScript Problem: Using Set to Create a Non-duplicated Collection
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?