37

How can I get this hh:mm:ss from date object?

var d = new Date(); // for now
datetext = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();

I get this result below sometimes,

12:10:1

which should be

12:10:01

I assume this happens to the hour and minute as well.

So I am after this

01:01:01

Not this 1:1:1

3

4 Answers 4

82

Solution - (tl;dr version)

datetext = d.toTimeString().split(' ')[0]

Explanation:

toTimeString returns the complete time. We split it by space to get the time component only, then take the first value which is of use. :)

Complete Flow:

d = new Date();
// d is "Sun Oct 13 2013 20:32:01 GMT+0530 (India Standard Time)"
datetext = d.toTimeString();
// datestring is "20:32:01 GMT+0530 (India Standard Time)"
// Split with ' ' and we get: ["20:32:01", "GMT+0530", "(India", "Standard", "Time)"]
// Take the first value from array :)
datetext = datetext.split(' ')[0];

Note: This does not require you to include any external files or libraries hence time required for execution would be faster.

3
  • Thanks man, for short and sweet solution. :) Commented Oct 7, 2020 at 6:32
  • >>datetext = datetext.split(' ')[0]; This return "20:32:01", is it possible to only HH:MM and not HH:MM:SS ? @sunny-r-gupta
    – Baadshah
    Commented Mar 1, 2022 at 14:04
  • @adi , sure, once you have HH:MM:SS, simply drop the last 3 🤷‍♂️ Commented Mar 21, 2022 at 20:48
19

I'd suggest you to checkout some date/time library. Moment.js is one good example.

You could do simply as below

var d = new Date();
var formatteddatestr = moment(d).format('hh:mm:ss a');

Here is an example from fiddle

4
  • That fiddle gave me "04:07:37 pm" instead of the "16:07:37" requested in the question.
    – Ivan
    Commented Jun 8, 2017 at 15:10
  • I suggest you go through the documentation. Sure you can manipulate the way you want if you change the formatter.
    – Jaya Mayu
    Commented Jun 12, 2017 at 4:14
  • 1
    Try HH instead of hh for 24h times.
    – cabhara
    Commented Jan 8, 2019 at 17:18
  • 1
    'a' gives am/pm. 'A' will give AM/PM Commented May 11, 2020 at 12:36
16

You can use this code snippet. I also added it to jsfiddle and added type-safer comments, so make sure to check out the fiddle.

If you're using jQuery, call it within your ready function. Otherwise you could use plain JavaScript to update the value of your field.

$(document).ready(function(){
    var d = new Date();
    var formatted_time = time_format(d);
    $('#yourTimeField').text(formatted_time);
});

Add these two methods to your script (you could also paste it in a single file, like it is in the jsfiddle code snippet):

function time_format(d) {
    hours = format_two_digits(d.getHours());
    minutes = format_two_digits(d.getMinutes());
    seconds = format_two_digits(d.getSeconds());
    return hours + ":" + minutes + ":" + seconds;
}

function format_two_digits(n) {
    return n < 10 ? '0' + n : n;
}

That's it :) Hope it helps :)

2
  • 1
    I prefer this answer rather than the accepted one because here you are using real hours, minutes and seconds instead to "blind trim" the string. Commented May 15, 2014 at 13:13
  • Just be vary that by minutes = you are setting minutes inside global namespace, I think that var minutes = would be better (or const minutes = in ES6). Commented Jan 12, 2022 at 10:40
1

You can also try using http://momentjs.com/. A javascript date library for parsing, validating, manipulating, and formatting dates.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.