Get the current time and the time one year later (China Standard Time) time processing

get current time

/**
 * 获取当前时间
 * @param timeStr 时分秒 字符串 格式为 xx:xx:xx
 */
export function getNowDateTime(timeStr) {
  let now = new Date();
  let year = now.getFullYear(); //得到年份
  let month = (now.getMonth() + 1).toString().padStart(2, "0"); //得到月份
  let day = now.getDate().toString().padStart(2, "0"); //得到日期

  if (timeStr == undefined) {
    return `${year}-${month}-${day}`;
  }
  let hours = now.getHours().toString().padStart(2, "0") // 得到小时;
  let minutes = now.getMinutes().toString().padStart(2, "0") // 得到分钟;
  let seconds = now.getSeconds().toString().padStart(2, "0") // 得到秒;
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

Get the time one year later, the time format is xxxx-xx-xx. Click the current component through the time component, and the next component is one year later, but the value obtained is wrong, and now it is processed by the code time method

 

/**
 * 获取一年后时间
 * @param timeStr 时分秒 字符串 格式为 xx:xx:xx
 */
export function getAfterDateTime(timeStr) {
  let now = new Date(timeStr);
  // console.log(now, '一年后时间')
  let year = now.getFullYear() + 1; //得到年份
  let month = (now.getMonth() + 1).toString().padStart(2, "0"); //得到月份
  let day = (now.getDate() - 1).toString().padStart(2, "0"); //得到日期

  if (month == '01' && day == '00') {
    year = now.getFullYear(); //得到年份
    month = '12';
    day = '31'
  } else if (day == '00') {
    year = now.getFullYear() + 1; //得到年份
    month = (now.getMonth()).toString().padStart(2, "0"); //得到月份;
    if (month == '01' || month == '03' || month == '05' || month == '07' || month == '08' || month == '10' || month == '12') {//大月
      day = '31'
    } else if (month == '04' || month == '06' || month == '09' || month == '11') {//小月
      day = '30'
    } else if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {//瑞年
      day = '29'
    } else {//平年
      day = '28'
    }
  } else {
    year = now.getFullYear() + 1; //得到年份
    month = (now.getMonth() + 1).toString().padStart(2, "0"); //得到月份
    day = (now.getDate() - 1).toString().padStart(2, "0"); //得到日期
  }
  // console.log(year, month, day, '年月日')

  // if (timeStr == undefined) {
  return `${year}-${month}-${day}`;
  // }
  // let hours = now.getHours().toString().padStart(2, "0") // 得到小时;
  // let minutes = now.getMinutes().toString().padStart(2, "0") // 得到分钟;
  // let seconds = now.getSeconds().toString().padStart(2, "0") // 得到秒;
  // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

 

Guess you like

Origin blog.csdn.net/m0_65274248/article/details/126843870