(一)日期字符串转换成时间戳
/**
* 日期字符串转换成时间戳
* @param dateStr 如:2014-05-08 00:22:11
*/
function get_unix_time(dateStr){
var newstr = dateStr.replace(/-/g,'/');
var date = new Date(newstr);
var time_str = date.getTime().toString();
return time_str.substr(0, 10);
}
setTimeout(function(){
//js代码
}, 2000);
(二)时间戳生成日期格式
/**
* 时间戳生成日期格式
*/
function getLocalTime(time){
if(time > 0){
var dateStr = new Date(time * 1000);
var str = "" + dateStr.getFullYear() + "-";
var m = dateStr.getMonth()+1;
m = m < 10 ? "0" + m : m;
str += m +"-";
var d = dateStr.getDate();
d = d < 10 ? "0" + d : d;
str += d;
return str;
}else{
return '末知时间';
}
}
(三)获取最近12个月
/**
* 获取最近12个月
*/
var yugi = function (now) {
var mydate = new Date(now.replace(/[^\d]/g, "/") + "/1");
var result = [now];
for(var i = 0; i < 11; i++) {
mydate.setMonth(mydate.getMonth() - 1);
var m = mydate.getMonth() + 1;
m = m < 10 ? "0" + m : m;
result.push(mydate.getFullYear() + "/" + m +"月");
}
return result;
}