vue时间格式总结以及转换方法详解

 更新时间:2022年12月12日 09:37:41   作者:EvaY_Yang  
项目中后台返回的时间有多种形式,时间戳、ISO标准时间格式等,我们需要转化展示成能看的懂得时间格式,下面这篇文章主要给大家介绍了关于vue时间格式总结以及转换方法的相关资料,需要的朋友可以参考下

前言

vue框架中我们常常用el-date-picker标签来显示和选择时间,那么,常见的时间的格式包含年-月-日(yyyy-MM-dd)、年-月-日 时-分-秒(yyyy-MM-dd HH-mm-ss)、标准时间格式以及时间戳。那么今天我们就来总结一下常用的获取方法和它们之间的转换方法。

一、获取当前时间

先看效果:

Ⅰ. 格式:年-月-日 时-分-秒(yyyy-MM-dd HH-mm-ss)

<template>
    <div>
		<h1>vue 时间格式常见应用</h1>
		<h3>获取当前时间(格式:年月日时分秒):{{time}}</h3>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				time:''
			}
		},
		created() {
			this.getNowTime();
		},
		methods: {
			getNowTime(){
				const yy = new Date().getFullYear()
				const MM = (new Date().getMonth() + 1) < 10 ? '0' + (new             
                    Date().getMonth() + 1) : (new Date().getMonth() + 1)
				const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new 
                    Date().getDate()
				const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new 
                    Date().getHours()
				const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : 
                     new Date().getMinutes()
				const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : 
                     new Date().getSeconds()
				this.time = yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss;
			}
		}
	}
</script>

Ⅱ.格式:标准时间

<template>
    <div>
		<h1>vue 时间格式常见应用</h1>
		<h3>获取当前标准时间(格式:年月日时分秒):{{standard_time}}</h3>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				standard_time:''
			}
		},
		created() {
			this.getGMTtime();
		},
		methods: {
			getGMTtime(){
				this.standard_time =new Date();
			}
		}
	}
</script>

Ⅲ.格式:时间戳

<template>
    <div>
		<h1>vue 时间格式常见应用</h1>
		<h3>获取当前时间的时间戳:{{current_timestamp}}</h3>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				current_timestamp:''
			}
		},
		created() {
			this.getnowtimestamp();
		},
		methods: {
			getnowtimestamp(){
				var date = new Date();
				this.current_timestamp = Date.parse(date)
			}
		}
	}
</script>

二、时间格式之间的转换

效果:

Ⅰ.年-月-日 时-分-秒格式转换成标准时间

<template>
   <h1>时间格式之间的转换</h1>
	<h3>1.年月日时分秒格式转换成标准时间</h3>
	<div style="display: flex;">
		<h5>假如将"2022-08-17 09:54:48"转换成标准时间格式,则标准格式为:</h5>
		<h4>{{conversion_time}}</h4>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				conversion_time: new Date('2022-08-17 09:54:48')
			}
		}
	}
</script>

Ⅱ.标准时间转换成年-月-日 时-分-秒格式

<template>
    <h1>时间格式之间的转换</h1>
    <h3>2.标准时间转换成年月日时分秒格式</h3>
    <div style="display: flex;">
		<h5>假如将"Wed Aug 17 2022 09:54:48 GMT+0800 (中国标准时间)"转换成年月日时分秒格式,则        
        写为:</h5>
		<h4>{{conversion_time1}}</h4>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				conversion_time1:'',
			}
		},
        created() {
			this.gettime();
		},
        methods: {
			gettime(){
				var date = new Date('Wed Aug 17 2022 09:54:48 GMT+0800 (中国标准时间)');
				var y = date.getFullYear();
				var m = date.getMonth() + 1;
				m = m < 10 ? ('0' + m) : m;
				var d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				var h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				var min = date.getMinutes();
				min = min < 10 ? ('0' + min) : min;
				var s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s;
				this.conversion_time1 = y + '-' + m + '-' + d + ' ' + h + ':' + min + ':'             
                + s;
			}
        }
	}
</script>

Ⅲ.年-月-日 时-分-秒格式转换成时间戳

<template>
    <h3>3.年月日时分秒格式转换成时间戳</h3>
    <div style="display: flex;">
        <h5>假如将"2022-08-17 09:54:48"转换成时间戳,则写为:</h5>
		<h4>{{conversion_time2}}</h4>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				conversion_time2:Date.parse('2022-08-17 09:54:48')
			}
		}
	}
</script>

这时你是不是有点困惑怎么来判断转换的时间戳是否正确呢,我们可以通过在线的转换工具来转换检测,转换工具网址:时间戳(Unix timestamp)转换工具 - 在线工具

 那下面我们来检测一下:

 所以转换的是没有问题的!

补充:vue中将任意格式的日期转换为年月日形式(yyyy-MM-dd)

time.js

export const formatDateTime = () => {
    let date = new Date();
    let timeStr = date.getFullYear() + '-';
    timeStr += date.getMonth() + 1 + '-';
    timeStr += date.getDate();
    return timeStr;
}

页面中使用

import { formatDateTime } from './time'

mounted() {
  console.log('当前时间:'+ formatDateTime(new Date().getTime()))     
}  

总结

到此这篇关于vue时间格式总结以及转换的文章就介绍到这了,更多相关vue时间格式转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue实现多个元素或多个组件之间动画效果

    vue实现多个元素或多个组件之间动画效果

    这篇文章主要为大家详细介绍了vue实现多个元素或多个组件之间动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • vue3.2最新语法使用socket.io实现即时通讯详解

    vue3.2最新语法使用socket.io实现即时通讯详解

    这篇文章主要为大家介绍了vue3.2最新语法使用socket.io实现即时通讯详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • electron中使用本地数据库的方法详解

    electron中使用本地数据库的方法详解

    众所周知,electron是可以开发桌面端的框架,那我们有一些数据不想让别人看到,只能在自己的电脑上展示时怎么办呢,这个时候就可以用到本地数据库,本文将以sqlite3为例介绍一下electron如何使用本地数据库
    2023-10-10
  • vue.js选中动态绑定的radio的指定项

    vue.js选中动态绑定的radio的指定项

    这篇文章主要介绍了vue.js选中动态绑定的radio的指定项,需要的朋友可以参考下
    2017-06-06
  • Vue选项之propsData传递数据方式

    Vue选项之propsData传递数据方式

    这篇文章主要介绍了Vue选项之propsData传递数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • 手摸手教你实现Vue3 Reactivity

    手摸手教你实现Vue3 Reactivity

    本文主要介绍了手摸手教你实现Vue3 Reactivity,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 解决vue路由发生了跳转但是界面没有任何反应问题

    解决vue路由发生了跳转但是界面没有任何反应问题

    这篇文章主要介绍了解决vue路由发生了跳转但是界面没有任何反应问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Vue中的event对象介绍

    Vue中的event对象介绍

    这篇文章介绍了Vue中的event对象,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • vue3使用ref的性能警告问题

    vue3使用ref的性能警告问题

    这篇文章主要介绍了vue3使用ref的性能警告问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • 基于Vue2的独立构建与运行时构建的差别(详解)

    基于Vue2的独立构建与运行时构建的差别(详解)

    下面小编就为大家分享一篇基于Vue2的独立构建与运行时构建的差别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12

最新评论