Sono nuovo nel mondo javascript e non ho trovato alcuna informazione su questa notazione. L'ho trovato in quell'argomento (vedi la risposta): Convert HH:MM:SS string to seconds only in javascript.Cosa significa (+ var) significa
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
Inoltre, desideravo utilizzare quel codice per convertire la stringa "HH: MM: SS" in secondi. Ma sembra pericoloso per me. Se l'utente inserisce 'XX: 03: SS', il valore sarà NaN che non è corretto (almeno per me). Così ho deciso di migliorato con:
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;
}
Quindi la mia domanda rimane ancora, che cosa fa (+ var) significa.
saluti,
La sua forma più breve per convertire in numero – Satpal