Is there a way to filter out the seconds in javascript of a timestring and only parse them when they are not :00 ?
Like 12:00:00 should be parsed as 12:00 Like 12:00:01 should be parsed as 12:00:01
Is there a way to filter out the seconds in javascript of a timestring and only parse them when they are not :00 ?
Like 12:00:00 should be parsed as 12:00 Like 12:00:01 should be parsed as 12:00:01
If you would put this in a function, I think it'll work fine:
var d = new Date(); //get date
h = d.getHours(); //get hours, minutes and seconds
m = d.getMinutes();
s = d.getSeconds();
if (h < 10) { // In case an hour, a minute or a second is less than 10 (so 1 decimal),
h = "0" + h; // an extra '0' will be added.
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
if (s == 0) { // If 'seconds' is equal to zero, it'll only return the hours and minutes.
return(h + ":" + m);
} else {
return(h + ":" + m + ":" + s);
}
It's long, I know, I'm sure there are shorter versions of this.