var date=new Date();
date.getYear(); //獲取當前年份(2位)
date.getFullYear(); //獲取完整的年份 (4位,2017)
date.getMonth(); //獲取當前月份 (0-11 0代表1月)
date.getDate(); //獲取當前日 (1-31)
date.getDay(); //獲取當前星期X (0-6 0代表星期天)
date.getTime(); //獲取當前時間的時間戳 (從1970.1.1 開始的毫秒數)
date.getHours(); //獲取當前小時數 (0-23)
date.getMinutes(); //獲取當前分鐘數(0-59)
date.getSeconds(); //獲取當前秒數(0-59)
date.getMilliseconds(); //獲取當前毫秒數(0-999)
date.toLocaleDateString(); //獲取當前日期 默認格式 2017/9/7
date.toLocaleTimeString(); //獲取當前時間 默認格式 上午10:45:23
date.toLocaleString(); //獲取當前日期和事件 默認格式 2017/9/7 上午10:45:23
Date.prototype.Format=function(fmt){
var o={
"M+":this.getMonth()+1,//月份
"d+":this.getDate(),//日
"H+":this.getHours(),//小時
"m+":this.getMinutes(),//分
"s+":this.getSeconds(),//秒
"q+":Math.floor((this.getMonth()+3)/3),//季度
"S+":this.getMilliseconds()//毫毛
};
if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
調用
var time=new Date().Format("yyyy-MM-dd");
var time=new Date().Format("yyyy-MM-dd HH:mm:ss");