日期相关的工具函数

1、时间戳转自定义格式时间

export const dateRegExp = (time, strText) => {
  const date = new Date(time)
  if (/(y+)/.test(strText)) {
    strText = strText.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  const dataType = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const typeKey in dataType) {
    if (new RegExp(`(${typeKey})`).test(strText)) {
      const typeValue = dataType[typeKey] + ''
      strText = strText.replace(RegExp.$1, typeValue.padStart(2, 0))
    }
  }
  return strText
}

2、格式化距离现在已过去的时间

export function formatPassTime(startTime) {
    var currentTime = Date.parse(new Date()),
        time = currentTime - startTime,
        day = parseInt(time / (1000 * 60 * 60 * 24)),
        hour = parseInt(time / (1000 * 60 * 60)),
        min = parseInt(time / (1000 * 60)),
        month = parseInt(day / 30),
        year = parseInt(month / 12);
    if (year) return year + "年前"
    if (month) return month + "个月前"
    if (day) return day + "天前"
    if (hour) return hour + "小时前"
    if (min) return min + "分钟前"
    else return '刚刚'
}

3、两个不同格式的日期是否为同一天

export function isSameDay(d1, d2) {
    if (!d2) {
        d2 = new Date();
    }
    var d1_year = d1.getFullYear(),
        d1_month = d1.getMonth() + 1,
        d1_date = d1.getDate();
    var d2_year = d2.getFullYear(),
        d2_month = d2.getMonth() + 1,
        d2_date = d2.getDate()

    return d1_date === d2_date && d1_month === d2_month && d1_year === d2_year;
}

4、是否是今天

export function isTodayDate(time) {
    if (new Date(time).toDateString() === new Date().toDateString()) {
        return true;
    }
    return false;
}

var date=new Date()
var date1=new Date(“2021-08-26 15:15:15”)
var date2=new Date(2021,9,18,23,15,23)

从1970/01/01到现在的毫秒值
var date = Date.now()
var date = +new Date()
var date = new Date().getTime()
var date = new Date().valueOf()
var date=Date.parse(new Date())
毫秒值 获取该毫秒值对应的时间日期
var date =new Date(Date.now());

getDate() 获取日 1-31
getDay () 获取星期 0-6(0代表周日)
getMonth () 获取月 0-11(1月从0开始)
getFullYear () 获取完整年份(浏览器都支持)
getHours () 获取小时 0-23
getMinutes () 获取分钟 0-59
getSeconds () 获取秒 0-59
getMilliseconds () 获取毫秒 (1s = 1000ms)
getTime () 返回累计毫秒数(从1970/1/1午夜)

将标准时间转成 年月日时分秒格式
function formatDate(date) {
const formatTen = (num) => {
return num > 9 ? (num + “”) : (“0” + num);
}
var date = new Date(date)
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return year + “-” + formatTen(month) + “-” + formatTen(day)+ " " +formatTen(hour)+ “:” +formatTen(minute)+ “:” +formatTen(second);
}

cont time = “2021年06月02日 05时03分02秒”
time.replace(/[^0-9]/mg, ‘’).match(/.{8}/); //20210602
time.replace(/(\d{4}).(\d{1,2}).(\d{1,2}).+/mg, ‘$1-$2-$3’) //2021-06-02

日期的现实形式
new Date(“month dd,yyyy hh:mm:ss”);
new Date(“month dd,yyyy”);
new Date(“yyyy/MM/dd hh:mm:ss”);
new Date(“yyyy/MM/dd”);
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值