Reputation: 23
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
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
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
Reputation: 23
Fixed it by using this code new Date(seconds * 1000).toISOString().substr(11, 5)
Upvotes: 0