Home >Web Front-end >JS Tutorial >A small example of converting date to timestamp in javascript_javascript skills

A small example of converting date to timestamp in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:39:561087browse

Copy code The code is as follows:

/**
* Convert date to timestamp
* Date format 2011-02-02 21:12:13
* time_str: Date period 2011-02-02
* fix_time: Time period 21:12: 13
*/
function strtotime(time_str, fix_time) {
var time = (new Date()).getTime();

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;
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn