我是新来的JavaScript世界,我还没有找到任何有关这种表示法的信息。 我在那个话题中找到了它(请参阅答案):Convert HH:MM:SS string to seconds only in javascript。(+ var)是什么意思
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
此外我想用该代码将'HH:MM:SS'字符串转换为秒。但是,这对我来说似乎并不安全。如果用户插入'XX:03:SS',则该值将是NaN,这是不正确的(至少对我而言)。所以我决定用它改进:
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;
}
所以我的问题仍然存在,什么是(+ VAR)的意思。
问候,
它最短的格式转换为数字 – Satpal