js 時間

//2019-03-20
  var date = new Date();//Wed Mar 20 2019 11:46:48 GMT+0800 (中國標準時間)
  var date = new Date().getTime();// 返回的是1970到如今的毫秒數,時間戳   1553053625688
  
  var year = date.getFullYear();//年份 2019
  var month = date.getMonth()+1;//月份  3
  var day = date.getDate(); //天  20
  
//獲取當前時間,格式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;
}
//js獲取當前日期先後N天的方法:
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m_ = (dd.getMonth()+1).toString();
    var m = '';
    m_.length >1 ? m =m_ : m ='0'+m_;
    var d_ = dd.getDate().toString();
    var d = '';
    d_.length > 1 ? d=d_ : d='0'+d_;
    return y+"-"+m+"-"+d;
}
//時間轉換成時間戳
function TimeChange(par) {
    var par_new = par.replace(/-/g, '/');
    var par_time = new Date(par_new);
    var Time = '';
    if(par == ''){
        Time = 0;
    }else{
        Time = par_time.getTime();
    }
    return Time;
}
//傳入年份和月份 獲取該年對應月份的天數
 getdaysinmonth(year,month){
  var thisDate = new Date(year,month,0); //當天數爲0 js自動處理爲上一月的最後一天
  return thisDate.getDate();
}
相關文章
相關標籤/搜索