Skip to content

年 月 辰

js
 const date = new Date();
        this.nowDate = date.getFullYear() //+ "年" //+ (date.getMonth() + 1) + "月" + date.getDate() + "日";
        this.yueData = date.getMonth() + 1  // (date.getMonth() + 1) + "月"
        console.log(this.yueData, 'yueData');
        console.log(this.nowDate + 1, 'nowDate');
        var d = new Date()
        var y = d.getFullYear()-1
        var m = d.getFullYear() + 1
        this.searchFormData.nd = String(y)
 const date = new Date();
        this.nowDate = date.getFullYear() //+ "年" //+ (date.getMonth() + 1) + "月" + date.getDate() + "日";
        this.yueData = date.getMonth() + 1  // (date.getMonth() + 1) + "月"
        console.log(this.yueData, 'yueData');
        console.log(this.nowDate + 1, 'nowDate');
        var d = new Date()
        var y = d.getFullYear()-1
        var m = d.getFullYear() + 1
        this.searchFormData.nd = String(y)

获取当日的日期

获取格式 格式 yyyy-MM-dd

js
 let date = new Date(),
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
    const hours = (date.getHours());//时
    const minutes = (date.getMinutes());//分
    const seconds = (date.getSeconds());//秒
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`,)
打印结果 : 2023-11-24
 let date = new Date(),
    year = date.getFullYear(), //获取完整的年份(4位)
    month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
    strDate = date.getDate() // 获取当前日(1-31)
    const hours = (date.getHours());//时
    const minutes = (date.getMinutes());//分
    const seconds = (date.getSeconds());//秒
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`,)
打印结果 : 2023-11-24

获取当前时间年月日时分秒

js
  var date = new Date();
  var year = date.getFullYear();
  /* 在日期格式中,月份是从0开始的,因此要加0
   * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
   * */
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  console.log('当前时间年月日时分秒', year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
  var date = new Date();
  var year = date.getFullYear();
  /* 在日期格式中,月份是从0开始的,因此要加0
   * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
   * */
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  console.log('当前时间年月日时分秒', year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);

禁止点击大于开始日期 和 不能选择今天往后的日期

js
结束日期绑定  :disabled-date="disabledDate"
const disabledDate = (time) => {
  return time.getTime() < new Date(form.value.ksrq) - 8.64e7;
  return time.getTime() > Date.now()  //不能选择今天往后的日期
  // return time.getTime() < Date.now() - 8.64e7; // 8.64e7 毫秒数代表一天
}
结束日期绑定  :disabled-date="disabledDate"
const disabledDate = (time) => {
  return time.getTime() < new Date(form.value.ksrq) - 8.64e7;
  return time.getTime() > Date.now()  //不能选择今天往后的日期
  // return time.getTime() < Date.now() - 8.64e7; // 8.64e7 毫秒数代表一天
}

当天日期 往前推7天 年月日时分秒

js
var currentDate = new Date();
  // 将当前日期往前推7天
currentDate.setDate(currentDate.getDate() - 7);

// 获取推算后的年、月、日、时、分、秒
var year1 = currentDate.getFullYear();
var month1 = padZero(currentDate.getMonth() + 1);
var day1 = padZero(currentDate.getDate());
var hours1 = padZero(currentDate.getHours());
var minutes1 = padZero(currentDate.getMinutes());
var seconds1 = padZero(currentDate.getSeconds());
// year1 + "-" + month1 + "-" + day1 + " " + hours1 + ":" + minutes1 + ":" + seconds1,
var currentDate = new Date();
  // 将当前日期往前推7天
currentDate.setDate(currentDate.getDate() - 7);

// 获取推算后的年、月、日、时、分、秒
var year1 = currentDate.getFullYear();
var month1 = padZero(currentDate.getMonth() + 1);
var day1 = padZero(currentDate.getDate());
var hours1 = padZero(currentDate.getHours());
var minutes1 = padZero(currentDate.getMinutes());
var seconds1 = padZero(currentDate.getSeconds());
// year1 + "-" + month1 + "-" + day1 + " " + hours1 + ":" + minutes1 + ":" + seconds1,

反快餐主义者