时间戳转年月日补0

97 阅读1分钟

代码块

<template>
  <view class="text-area">
    <text class="title">{{ option.createDate }}</text>
  </view>
</template>

<script>
  // import Vue from 'vue';

export default {
  data() {
    return {
      option: {},
    };
  },
    onLoad(option) {
    this.option = option;
    this.option.createDate = this.timeFormat(parseInt(this.option.createDate));//因为通过URL传过来的是字符串
  },
  methods: {
    timeFormat(timestamp) {
      var time = new Date(timestamp);
      var year = time.getFullYear();
      var month = (time.getMonth() + 1 + "").padStart(2, 0);
      var date = (time.getDate() + "").padStart(2, 0);
      var hours = (time.getHours() + "").padStart(2, 0);
      var minutes = (time.getMinutes() + "").padStart(2, 0);
      var seconds = (time.getSeconds() + "").padStart(2, 0);
      return (
        year +
        "-" +
        month +
        "-" +
        date +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds
      );
    },
  },
};
</script>

<style>

</style>