1.js获取当前时间 年-月-日 时:分:秒
function getNowFormatDate() {//获取当前时间
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
2. js获取当前时间 年-月-日
//获取当前时间,格式YYYY-MM-DD
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
3. js获取当前时间 时-分-秒
function getHourDate(){
var myDate = new Date(); //实例一个时间对象;
var hour=myDate.getHours(); //获取系统时,
var min=myDate.getMinutes(); //分
var seconds="00"; //秒
// console.log("获取时="+hour+"=分="+min+"=秒="+seconds);
if (hour<10) {
hour= "0"+hour;
}
if(min<10){
min="0"+min;
}
var mytime=hour+":"+min+":"+seconds;
// console.log("返回的时分秒="+mytime);
return mytime;
}
4. 获取距离当前几天的时间
//n为你要传入的参数,当前为0,前一天为-1,后一天为1
function getBeforeDate(n){
var date = new Date() ;
var year,month,day ;
date.setDate(date.getDate()+n);
year = date.getFullYear();
month = date.getMonth()+1;
day = date.getDate() ;
s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day) ;
return s ;
}