Home >Web Front-end >JS Tutorial >A small example of converting date to timestamp in javascript_javascript skills
if(time_str) {//With date segment
var str = time_str.split('-');
if (3 === str.length) {
var year = parseInt( str[0]) - 0;
var month = parseInt(str[1]) - 0 - 1; //The month starts from 0
var day = parseInt(str[2]) - 0;
if(fix_time) {//There is a time period
var fix = fix_time.split(':');
if (3 === fix.length) {
var hour = parseInt( fix[0]) - 0;
var minute = parseInt(fix[1]) - 0;
var second = parseInt(fix[2]) - 0;
time = (new Date(year , month, day, hour, minute, second)).getTime();
}
} else {
time = (new Date(year, month, day)).getTime();
}
}
}
//The timestamp obtained by getTime() reaches the number of milliseconds
time = time / 1000;//Go to the number of seconds
return time;
}