vue 时间格式总结及转换

本文详细介绍了在Vue框架中如何获取和转换不同时间格式,包括当前时间(年-月-日时-分-秒、标准时间、时间戳)以及它们之间的相互转换方法。通过示例代码展示了如何实现年月日时分秒格式转标准时间、标准时间转年月日时分秒格式以及时间戳转换。同时提供了在线时间戳转换工具进行验证,确保转换的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        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)转换工具 - 在线工具

 那下面我们来检测一下:

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

### Python 后端时间格式化 为了在Python后端进行时间格式化,可以利用`datetime`库来操作日期和时间对象。对于返回给前端的数据,建议将时间转换为ISO 8601标准字符串形式,这样便于跨平台解析。 ```python from datetime import datetime, timezone def format_time_for_mobile(): current_time = datetime.now(timezone.utc) formatted_time = current_time.isoformat() # ISO 8601 格式 return {"time": formatted_time} ``` 上述代码片段展示了如何获取当前UTC时间和将其转化为ISO 8601格式[^1]。 ### Vue3 前端时间格式化 在Vue3应用中展示经过格式化的本地时间,可以通过安装第三方库如`date-fns`或使用内置的JavaScript `Date`对象来进行处理: #### 安装 date-fns 库 如果选择使用`date-fns`,首先需要通过npm或其他包管理工具安装该库: ```bash npm install date-fns ``` 接着可以在组件内部引入并使用这个库的功能: ```javascript import { defineComponent } from 'vue' import { formatDistanceToNowStrict, parseISO } from 'date-fns' export default defineComponent({ props: { isoTime: String, }, computed: { readableTime() { const parsedTime = parseISO(this.isoTime); return formatDistanceToNowStrict(parsedTime) + ' ago'; } } }) ``` 这段代码接收来自服务器的一个ISO 8601时间戳作为属性,并计算出相对于现在的相对时间段表示法[^2]。 另外一种方法是直接依赖浏览器环境中的`Intl.DateTimeFormat()`函数,它可以很好地支持国际化需求: ```html <script setup> const props = defineProps(['isoTime']); </script> <template> <p>{{ new Date(isoTime).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }) }}</p> </template> ``` 这里假设传入的是一个有效的ISO 8601字符串,它会被解释为中国上海时区内的当地时间显示出来[^3]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值