Reputation: 174
friends. I have problem with my script. I need to convert 1:20:22 into 80:22. And it don't show seconds correct. I try this:
var eventTime = moment('1:12:22', 'hh:mm:ss');
var terribleTime = (eventTime.hour()*60) + eventTime.minute() + ':' + eventTime.seconds();
if I use "00" seconds it shows me only 1 zero, like that: "12:0":
Answer on my question, If somebody need the answer in future!!!
var terribleTime = moment(eventTime).hours()*60 + moment(eventTime).minutes() + ':' + moment(eventTime).format('ss');
Upvotes: 0
Views: 2888
Reputation: 2119
I did it without moment. It seemed too much involved using internal functions of moment and it is a heavy library wrt size.
const getMinutesSeconds = (seconds = 0) => {
const newMinutes = seconds >= 60 ? parseInt(seconds / 60, 10) : 0
const newSeconds = parseInt(seconds % 60, 10)
return newMinutes.toString().padStart(2, '0') + ":" + newSeconds.toString().padStart(2, '0')
}
console.log(getMinutesSeconds(1236))
console.log(getMinutesSeconds(65))
console.log(getMinutesSeconds(121))
console.log(getMinutesSeconds(180))
Here you will get output in mm:ss
format, appending 0 before if it is single digit.
Upvotes: 1
Reputation: 12452
Here is a working fiddle with your example:
JS - using Jquery 1.9.1 & momentjs 2.8.3 :
$(document).ready(function(){
var eventTime = moment('1:12:22', 'hh:mm:ss');
var terribleTime = moment(eventTime).hour()*60 + moment(eventTime).minute() + ':' + moment(eventTime).seconds();
console.log(terribleTime);
});
EDIT: In this fiddle you have annother approach. There is no need to create a moment for every hour,minute,seconds function as the eventTime is a moment object.
$(document).ready(function(){
var eventTime = moment('1:12:22', 'hh:mm:ss');
var terribleTime = eventTime.hour()*60 + eventTime.minute() + ':' + eventTime.seconds();
console.log(terribleTime);
});
This is tested too in chrome, ie, ff & safari.
Upvotes: 1