JS:日期操作


获取日期年月日时分秒

 const year = date.getFullYear(); // 年份,例如 2023
 const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1); // 月份,0-11,0 表示一月,11 表示十二月
 const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(); // 日期,1-31
 const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(); // 小时,0-23
 const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(); // 分钟,0-59
 const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds(); // 秒数,0-59
 const millisecond = date.getMilliseconds(); // 毫秒数,0-999
 const weekday = date.getDay(); // 星期几,0-6,0 表示周日,6 表示周六

增加或者是减少日期小时数

export function dateAddHour(date, hour) {
  if (date == null) return null
  var curTime = new Date(date)
  var addHour = curTime.setHours(curTime.getHours() + hour);
  return new Date(addHour)
}

增加或者是减少天数

export function dateAddDay(date, day) {
  if (date == null) return null
  var curTime = new Date(date);
  curTime.setDate(curTime.getDate() + day);
  return new Date(curTime)
}

格式化日期格式(yyyy-MM-dd HH:mm:ss)

export function formatDate1(date) {
  if (date == null) return null
  const year = date.getFullYear(); // 年份,例如 2023
  const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1); // 月份,0-11,0 表示一月,11 表示十二月
  const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(); // 日期,1-31
  const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(); // 小时,0-23
  const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(); // 分钟,0-59
  const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds(); // 秒数,0-59
  const millisecond = date.getMilliseconds(); // 毫秒数,0-999
  const weekday = date.getDay(); // 星期几,0-6,0 表示周日,6 表示周六
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}

格式化日期格式(yyyy-MM-dd)

export function formatDate2(date) {
  if (date == null) return null
  const year = date.getFullYear(); // 年份,例如 2023
  const month = date.getMonth() > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1); // 月份,0-11,0 表示一月,11 表示十二月
  const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(); // 日期,1-31
  const hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(); // 小时,0-23
  const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(); // 分钟,0-59
  const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds(); // 秒数,0-59
  const millisecond = date.getMilliseconds(); // 毫秒数,0-999
  const weekday = date.getDay(); // 星期几,0-6,0 表示周日,6 表示周六
  return year + '-' + month + '-' + day;
}

生成时间戳

export function getTimeStamp(date){
  return date.valueOf()
}

获取当月第一天

export function getDateFirst(date) {
  date.setDate(1);
  return formatDate2(date)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monly21

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

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

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

打赏作者

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

抵扣说明:

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

余额充值