时间格式转换JS

目录

时间格式整理:

格式转换:

1、获取GMT格式:

 2、GMT格式转换成时间戳:

3、 时间戳转换成GMT格式:

4、带时间格式转换成GMT格式:

格式互相转换:

方法一、先转换成中国标准时间格式,再转成yyyy-MM-dd hh:mm:ss

方法二、后台返回

获取当前年月日,时分秒,星期几

效果如下:​编辑



时间格式整理:

1、Unix时间戳,例如:1515260000000。

2、中国标准格式(格林威治标准时间GMT),例如:Sat Sep 01 2018 08:00:00 GMT+0800 (中国标准时间)。

3、UTC世界协调时间,例如:Mon, 29 Apr 2019 09:52:21 GMT。

4、ISO 8601,例如:2020-12-05T05:33:19Z  

                             或者    2004-05-03T17:30:08+08:00。


格式转换:

1、获取GMT格式:

new Date('2020-12-5')
console.log(new Date('2020-12-5'))

输出结果:

Sat Dec 05 2020 15:21:57 GMT+0800 (中国标准时间)

 2、GMT格式转换成时间戳:

Number(1515260000000)

3、 时间戳转换成GMT格式:

new Date(1515260000000)
console.log(new Date(1515260000000))

输出结果:

Sat Sep 01 2018 08:00:00 GMT+0800 (中国标准时间)

4、带时间格式转换成GMT格式:

new Date('2015-11-11')
console.log(new Date('2015-11-11'))

输出结果:

Mon Nov 11 2015 08:00:00 GMT+0800 (中国标准时间)

格式互相转换:

方法一、先转换成中国标准时间格式,再转成yyyy-MM-dd hh:mm:ss

export function formatDate(date, fmt) {
  let o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    S: date.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}

方法二、后台返回

index.vue

<template>
    <p v-for="(item_,index) in list" :key="index">
                  {
   
   { formatDate(item_.creattime) }}
    </p>
</template>
<script>
import { formatDate } from "../../../utils/index";
export default {
  data() {
    return {
        formatDate: formatDate, //时间格式转换

    }
  }




}
</script>

index.js

export function formatDate(cellValue) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}


获取当前年月日,时分秒,星期几

效果如下:

<template>
    <div class="navigation_item">
      <div class="time">{
   
   { dateTime }}</div>
      <div class="time_item">
        <div class="day">{
   
   { day }}</div>
        <div class="date">{
   
   { currentDate }}</div>
      </div>
    </div>
</template>
<script>
export default {
  data() {
    return {
      dateTime: "",
      day: "",
      currentDate: "",
    }
  },
  methods:{

        getTime() {
      var date = new Date();
      this.dateTime =
        date.getHours() +
        ":" +
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":" +
        (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
      if (date.getDay() == 0) {
        this.day = "星期天";
      } else if (date.getDay() == 1) {
        this.day = "星期一";
      } else if (date.getDay() == 2) {
        this.day = "星期二";
      } else if (date.getDay() == 3) {
        this.day = "星期三";
      } else if (date.getDay() == 4) {
        this.day = "星期四";
      } else if (date.getDay() == 5) {
        this.day = "星期五";
      } else if (date.getDay() == 6) {
        this.day = "星期六";
      }
      this.currentDate =
        date.getFullYear() +
        "." +
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) +
        "." +
        date.getDate();
    },

 },
   mounted(){
  
    this.getTime()
   }

 }

}
</script>

猜你喜欢

转载自blog.csdn.net/weixin_44126032/article/details/127326098