收藏
回答

如何将数据库中的时间戳转换成年月日的格式?

  <block wx:if="{{data.rate.createtime}}">
    <view class="info-content data-v-52157f4e">
      <view class="u-m-l-10 u-m-r-10 propose data-v-52157f4e">日期:{{data.rate.createtime}}</view>
    </view>
  </block>

目前页面显示为时间戳格式。

请问大神们如何将这个时间戳格式化成年月日的格式。

回答关注问题邀请回答
收藏

3 个回答

  • 启年
    启年
    2023-02-14

    最好是用wxs

    可以看我写的这篇文章

    https://developers.weixin.qq.com/community/develop/article/doc/000442c4a2c63809ac4f74cda56013

    2023-02-14
    有用
    回复
  • LUL
    LUL
    2023-02-14

    简单点的就是写个转换函数,如果使用场景多,就作为单独的文件引用。

    const formatTime = date => {
        var date = new Date(date);
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()
        return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
    };
    //格式化日期
    const formatDate = date => {
        var date = new Date(date);
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        return `${[year, month, day].map(formatNumber).join('-')}`
    };
    //格式化日期 6月1日
    const formatSimpleDate = date => {
        var date = new Date(date);
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        return `${[month, day].map(formatNumber).join('.')}`
    };
    // 生成当前日期字符串,精确到秒
    const formatTimeSecond = date => {
        var date = new Date(date);
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()
        return `${[year, month, day,hour, minute, second].map(formatNumber).join('')}`
    };
    
    2023-02-14
    有用
    回复 1
    • LUL
      LUL
      2023-02-14
      后期还有其他需求的,可以使用Day.js,Moment.js 等开源文件
      2023-02-14
      1
      回复
  • 旷奇艺
    旷奇艺
    2023-02-14

    https://dayjs.gitee.io/zh-CN/

    2023-02-14
    有用
    回复
登录 后发表内容