• 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;
                },
    
    {{ dateFormat(item.rukudate) }}

    或者

    {{ dateFormat(item.yuyuedate) }}

    //格式化处理 方式二:

    // 时间过滤器
            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;
                }
            },
    发表时间:{{ item.add_time | formatDate }}

    Element UI -- el-date-picker 日期组件设置默认值

    <el-date-picker

             style="width:23%"

             v-model="time"

             type="daterange"

             range-separator="至"

             start-placeholder="开始日期"

             end-placeholder="结束日期"

             value-format="yyyy-MM-dd"

    >el-date-picker>

    // 默认时间
            timeDefault () {
                let date = new Date()
                // 通过时间戳计算
                let defalutStartTime = date.getTime() - 7 * 24 * 3600 * 1000 // 转化为时间戳
                let defalutEndTime = date.getTime()
                let startDateNs = new Date(defalutStartTime) 
                let endDateNs = new Date(defalutEndTime)
                // 月,日 不够10补0
                defalutStartTime = startDateNs.getFullYear() + '-' + ((startDateNs.getMonth() + 1) >= 10 ? (startDateNs.getMonth() + 1) : '0' + (startDateNs.getMonth() + 1)) + '-' + (startDateNs.getDate() >= 10 ? startDateNs.getDate() : '0' + startDateNs.getDate())
                defalutEndTime = endDateNs.getFullYear() + '-' + ((endDateNs.getMonth() + 1) >= 10 ? (endDateNs.getMonth() + 1) : '0' + (endDateNs.getMonth() + 1)) + '-' + (endDateNs.getDate() >= 10 ? endDateNs.getDate() : '0' + endDateNs.getDate())
                return [defalutStartTime, defalutEndTime]
            }

  • 相关阅读:
    202303机器人三级理论部分精密解析
    浅谈java开启异步线程的几种方法(@Async,AsyncManager,线程池)
    win10没有改用本地账户登录选项怎么办
    企业数字化办公利器——华为云桌面Workspace
    Linux驱动开发:深入理解I2C时序
    [前端框架]-VUE(下篇)
    新建一个ARXML数据库文件
    功能基础篇7——Python基础数据结构与算法标准库
    pytorch训练加速技巧
    项目管理逻辑:为什么职能部门官僚主义气息浓重?
  • 原文地址:https://blog.csdn.net/tiankongxiao/article/details/126444652