获取昨天,今天,明天的时间

获取到昨天,今天,明天的时间,时间格式为  ------》2019-01-27      2019-01-28      2019-01-29

var yesterday = GetDateStr(-1);   //昨天
var today = GetDateStr(0);        //今天
var tomorrow = GetDateStr(+1);    //明天



function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; // 获取当前月份的日期
    m = m.toString();
    if (m.length == 1) {
        m = '0' + m;
    }
    var d = dd.getDate();
    d = d.toString();
    if (d.length == 1) {
        d = '0' + d;
    }
    return y + "-" + m + "-" + d;
}

获取今天的时间:要的是年月日,格式如"2019-08-30"

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

var now=getNowFormatDate();

console.log(now)===>"2019-08-30"
拿到的是当前天的日期

 

你可能感兴趣的:(js)