I'm new into the javascript world and I have not found any information about this notation. I found it in that topic (see the answer): Convert HH:MM:SS string to seconds only in javascript.
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
Also I wanted to use that code to convert 'HH:MM:SS' string to seconds. But, it seems unsafe for me. If the user inserts 'XX:03:SS', the value will be NaN which is not correct (at least for me). So I decided to improved it with:
function convertHHMMSSToSeconds(time) {
// initialize seconds
var seconds = 0;
//ensure time
if (!time) {
return seconds;
}
try {
var hmsTab = time.split(':'); // split it at the colons
// ensure that the hmsTab contains 3 values (hh,mm,ss)
if (!hmsTab || hmsTab.length !== 3) {
return seconds;
}
// initialize hh, mm and ss
var hh = hmsTab[0] > 0 && hmsTab[0] < 60? +hmsTab[0] : 0;
var mm = hmsTab[1] > 0 && hmsTab[1] < 60 ? +hmsTab[1] : 0;
var ss = hmsTab[2] > 0 && hmsTab[2] < 60 ? +hmsTab[2] : 0;
// return 0 if one variable is not valid
if(+hmsTab[0] !== hh ||+hmsTab[1] !== mm || +hmsTab[2] !== ss) {
return seconds;
}
// minutes are worth 60 seconds. Hours are worth 60 minutes.
seconds = (hh * 60 * 60) + (mm * 60) + ss;
}catch (error)
{
seconds = 0;
}
return seconds && seconds>0 ? seconds : 0;
}
So my question still remains, what does (+var) mean.
Regards,
The +
sign in front of a variable, will cast that variable to a number. Example:
var x = "3";
var y = x + 10; // 310
var z = +x + 10 // 13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With