好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

uniapp时间格式化处理实现

应用需求分析:前台页面有时需要展示YYYY-MM-DD格式,但后台却返回给我们YYYY-MM-DD hh:mm:ss、或者是一串字符

//格式化处理 方式一:
            dateFormat(time) {
                let date = new Date(time);
                let year = date.getFullYear();
                // 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
                let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                // 拼接
                // return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
                return year + "-" + month + "-" + day;
            },
<view v-if="item.rukudate">{{ dateFormat(item.rukudate) }}</view>

或者

<view v-if="item.yuyuedate" :class="dateFormat(item.yuyuedate) == day ? 'sameDay' : '' ">{{ dateFormat(item.yuyuedate) }}</view>
//格式化处理 方式二:

// 时间过滤器
? ? ? ? filters:{
? ? ? ? ? ? formatDate(date){
? ? ? ? ? ? ? ? console.log(date)
? ? ? ? ? ? ? ? let newDate = new Date(date);
? ? ? ? ? ? ? ? let year = newDate.getFullYear();
? ? ? ? ? ? ? ? let month = newDate.getMonth().toString().padStart(2,0);
? ? ? ? ? ? ? ? let day = newDate.getDay().toString().padStart(2,0);
? ? ? ? ? ? ? ? return year + '-' + month + '-' + day;
? ? ? ? ? ? }
? ? ? ? },
<view>发表时间:{{ item.add_time | formatDate }}</view>

PS:uniapp实现时间格式化,显示几分钟之前

<template>
?? ?<view class="content">
?? ??? ?<view>
?? ??? ??? ?<text>{{time}}</text>
? ? ? ? </view>
?? ?</view>
</template>

<script>
?? ?var dateUtils = require('components/util.js').dateUtils;
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?time:''
?? ??? ??? ?}
?? ??? ?},
?? ??? ?onLoad() {
?? ??? ??? ?uni.request({
?? ??? ??? ??? ?url: 'http://api.hnwlcm.com:8081/articleCover/Recommend/1?category=1',
?? ??? ??? ??? ?method: 'GET',
?? ??? ??? ??? ?success: res => {
?? ??? ??? ??? ??? ?console.log(res);
?? ??? ??? ??? ??? ?var newsList = res.data.extend.articles;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?// 时间问题
?? ??? ??? ??? ??? ?for (var i in newsList) {
?? ??? ??? ??? ??? ??? ?this.time = dateUtils.format(newsList[i].updateTime);
?? ??? ??? ??? ??? ??? ?// console.log(this.time)
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?});
?? ??? ?},
?? ??? ?methods: {
?? ??? ??? ?
?? ??? ?}
?? ?}
</script>

<style>
</style>

到此这篇关于uniapp时间格式化处理实现的文章就介绍到这了,更多相关uniapp 时间格式化内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于uniapp时间格式化处理实现的详细内容...

  阅读:51次