Home > Net >  How to return HH:mm only in date javascript
How to return HH:mm only in date javascript

Time:09-22

I'm converting seconds to time using this code,

var seconds = 8274;
new Date(seconds * 1000).toISOString().substr(11, 8)

And it will return like this 02:17:54

I need to return only hours and minutes only like this, 02:17

Please help.

CodePudding user response:

Fixed it by using this code new Date(seconds * 1000).toISOString().substr(11, 5)

CodePudding user response:

Use the methods getHours() and getMinutes() on the date object like.

const d = new Date();
const h = d.getHours();
const m = d.getMinutes();

const t = h   ":"   m;

If you want it to be prefixed with a leading 0 when hours or minutes are lower than 10 use the length or verify <= 9.

  • Related