JS获取当前时间、及一周之前、一个月之前日期和获取当前时间戳及计算两个日期之间的时间戳

JS获取当前时间、及一周之前、一个月之前日期

 //获取当前时间戳
 const t1 = new Date().valueOf() // 第一种,推荐
const t2 = new Date().getTime() // 第二种,推荐
const t3 = Date.parse(new Date()) // 第三种,不推荐,精度差一些

 //获取当前日期
    getDay(day){
      var today = new Date();
      var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
      today.setTime(targetday_milliseconds); //注意,这行是关键代码
      var tYear = today.getFullYear();
      var tMonth = today.getMonth();
      var tDate = today.getDate();
      tMonth = this.doHandleMonth(tMonth + 1);
      tDate = this.doHandleMonth(tDate);
      return tYear+""+tMonth+""+tDate;
    },
    //获取当前月份
    doHandleMonth(month){
      var m = month;
      if(month.toString().length == 1){
        m = "0" + month;
      }
      return m;
    },
  //一周之前
  this.params.startDate=this.getDay(-7)
  //两周之前
  this.params.startDate=this.getDay(-14)
  //一个月之前
  this.params.startDate=this.getDay(-31)
  //当前时间
  this.params.endDate=this.getDay(0)
  

获取年月日加时分秒

getNowDate  () {
  var date = new Date();
  var sign2 = ":";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
  var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  var week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}

yyyyMMddHHmmssSSS最后在加一位1-10的随机数

function generateTimestamp() {
  const date = new Date();
  const year = date.getFullYear();
  const month = ("0" + (date.getMonth() + 1)).slice(-2);
  const day = ("0" + date.getDate()).slice(-2);
  const hour = ("0" + date.getHours()).slice(-2);
  const minute = ("0" + date.getMinutes()).slice(-2);
  const second = ("0" + date.getSeconds()).slice(-2);
  const millisecond = ("00" + date.getMilliseconds()).slice(-3);

  const randomNumber = Math.floor(Math.random() * 10) + 1;

  return year + month + day + hour + minute + second + millisecond + randomNumber;
}

计算两个日期之前的时间差

// 定义两个日期
var date1 = new Date("2022/01/01 00:00:00");
var date2 = new Date("2022/02/01 00:00:00");

// 计算两个日期之间的毫秒数差
var diff = Math.abs(date1.getTime() - date2.getTime());

// 将毫秒数转换为年、月、日、时、分、秒
var years = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
diff -= years * 1000 * 60 * 60 * 24 * 365;
var months = Math.floor(diff / (1000 * 60 * 60 * 24 * 30));
diff -= months * 1000 * 60 * 60 * 24 * 30;
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * 1000 * 60 * 60 * 24;
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / (1000 * 60));
diff -= minutes * 1000 * 60;
var seconds = Math.floor(diff / 1000);

console.log(years + "年" + months + "个月" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");

时间戳转换日期格式

//时间戳转换
  const returnTime=(timestamp:any)=>{
    var date = new Date(timestamp);
  var year = date.getFullYear();
  var month = ("0" + (date.getMonth() + 1)).slice(-2); // 前补0,确保月份始终是两位数
  var day = ("0" + date.getDate()).slice(-2);
  var hours = ("0" + date.getHours()).slice(-2);
  var minutes = ("0" + date.getMinutes()).slice(-2);
  var seconds = ("0" + date.getSeconds()).slice(-2);

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shero.李建业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值