uniapp封装日期时间

文章描述:

uniapp封装日期和时间以及使用方法

1、在utils目录下创建getDate.js文件

export default function getDateTime(){      //默认导出
        let date = new Date()
        let year = date.getFullYear()
        let mounth = date.getMonth()+1
        let day = date.getDate()
        let time = date.getHours()
        let min = date.getMinutes()
        let sec = date.getSeconds()
        let str = year + '-' + mounth +'-' + day + ' ' + time +':' + min + ':' + sec
        return str
}
 
export  function getDate(){
        let date = new Date()
        let year = date.getFullYear()
        let mounth = date.getMonth()+1
        let day = date.getDate()
        let str = year + '-' + mounth +'-' + day
        return str
}

2、vue使用

<template>
	<view class="content">
	    <!-- 2023-2-26 20:40:26 -->
            <view class="">{{getDateTime}}</view>   
            <!-- 2023-2-26  -->
            <view class="">{{getDate}} </view>
	</view>
</template>

<script>
import getDateTime,{getDate}  from '@/utils/getDate.js' 
export default {
    data() {
        return {
            getDateTime:getDateTime(),   
            getDate:getDate()
        }
    },
	onShow() {
		console.log(getDate())
	}
}
</script>

 

发布时间:2023/02/26

发表评论