sadique mohammed
sadique mohammed

Reputation: 23

How to return HH:mm only in date javascript

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.

Upvotes: 0

Views: 1023

Answers (3)

WenTing
WenTing

Reputation: 1

let seconds = 11820;

new Date(seconds * 1000).toISOString();
 return `'1970-01-01T03:17:00.000Z'`

new Date(seconds * 1000).toISOString().substr(11, 8);
 return `'03:17:00'`

new Date(seconds * 1000).toISOString().substr(11, 5);
 return `'03:17'`, which you want :)

Upvotes: 0

Bojoer
Bojoer

Reputation: 948

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 padStart method.

Upvotes: 1

sadique mohammed
sadique mohammed

Reputation: 23

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

Upvotes: 0

Related Questions