首页 > 其他 > 详细

Data的方法

时间:2021-08-19 22:22:18      阅读:40      评论:0      收藏:0      [点我收藏+]

Date对象的基本知识点

技术分享图片
技术分享图片

获取时间,指定格式xxxx-xx-xx或者xxxx年xx月xx年

 /**
  * 获取操作时间,以 xxxx-xx-xx 的形式进行返回
  * @methods getDate
  * @param {string} dataType 返回时间的类型
  * @returns {string} 返回指定类型的时间
  */
getDate(dataType) {
    let year = new Date().getFullYear();
    let month = new Date().getMonth() + 1;
    let date = new Date().getDate();
    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (dataType == "xxxx-xx-xx") {
        return `${year}-${month}-${date}`;
    } else if (dataType == "xxxx年xx月xx日") {
        return `${year}年${month}月${date}日`
    }
 }

获取上一个自然日

/**
 * 获取上一个自然日
 * @methods getTotalAndTime
 * @returns {string} 返回上一个自然日
 */
getTotalAndTime() {
    let year = new Date().getFullYear();
    let month = new Date().getMonth() + 1;
    let date = new Date().getDate() - 1;
    if (date == 0) {
        date = new Date(new Date().setDate(date)).getDate();
        month = new Date(new Date().setMonth(month - 1)).getMonth();
        if (month == 0) {
            month = 12;
            year -= 1;
        }
    }
    if (date < 10) {
        date = `0${date}`
    }
    if (month < 10) {
        month = `0${month}`
    }
    return `${year}年${month}月${date}日`;
}

Data的方法

原文:https://www.cnblogs.com/mingli01/p/15162855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!