moment.js 封装时间格式化方法 将时间戳转换成日期格式

moment.js官网  Moment.js 中文网

下载包

npm install moment --save

引入及使用

import moment from 'moment';

//距离目前时间差
export const longAgo = (params) => {
    let time = moment(params).fromNow();
    if (time == 'a few seconds ago') {
        return '1分钟前'
    } else if (time.includes('minute')) {
        if (time.slice(0, 1) == 'a') {
            return `1分钟前`;
        }
        return `${parseInt(time)}分钟前`;
    } else if (time.includes('hour')) {
        if (time.slice(0, 1) == 'a') {
            return `1小时前`;
        }
        return `${parseInt(time)}小时前`;
    } else if (time.includes('day')) {
        if (time.slice(0, 1) == 'a') {
            return `1天前`;
        }
        return `${parseInt(time)}天前`;
    } else if (time.includes('months')) {
        if (time.slice(0, 1) == 'a') {
            return `1个月前`;
        }
        return `${parseInt(time)}个月前`;
    } else if (time.includes('year')) {
        if (time.slice(0, 1) == 'a') {
            return `1年前`;
        }
        return `${parseInt(time)}年前`;
    }
}

//导出指定格式时间
export const dateTime = (params) => {
    return moment(params).format("YYYY/MM/DD  HH:mm:ss");
}
//导出指定格式时间
export const dateYearMouth = (params) => {
    return moment(params).format("YYYY/MM");
}
//导出指定格式时间
export const dateMouthDay = (params) => {
    return moment(params).format("MM/DD");
}

//导出时间倒计时
export const autoEndTime = (params) => {
    let now = moment();
    let expiration = moment(params)
    const diff = expiration.diff(now);
    const diffDuration = moment.duration(diff);
    return `将在${diffDuration.days()}天${diffDuration.hours()}小时${diffDuration.minutes()}分钟${diffDuration.seconds()}秒后结束`
}

//导出发送消息时间
export const createMsgTime = (params) => {
    if (moment(params).format("YYYY/MM/DD") == moment().format("YYYY/MM/DD")) {
        return moment(params).format("HH:mm")
    } else if (moment(params).format("YYYY") == moment().format("YYYY")) {
        return moment(params).format("MM/DD HH:mm")
    } else {
        return moment(params).format("YYYY/MM/DD HH:mm")
    }
}



//检查时间是否以过期
export const isBefore = (time) => {
    return moment(time).isBefore(new Date())
}


//倒计时 
//定时器每秒调用一次
setInterval(() => {
      let start = moment(new Date());//获取开始时间
      let end = moment(new Date("2020/12/20"));//结束时间
      let diff = end.diff(start);//时间差
      let time = `${moment.duration(diff).hours()} - ${moment.duration(diff).minutes()} - ${moment.duration(diff).seconds()}`//格式化为需要的格式 这里是时分秒
      console.log(time)
    }, 1000)

将时间戳转换成日期格式

export const dateFormat = function (dateStr) {
    var date = new Date(dateStr*1000);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();

    return Y+M+D+h+m+s;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值