zoukankan      html  css  js  c++  java
  • JS 时间获取 (常用)

      最近在写的项目对日期获取的方法种类用的比较多,于是在此进行录入下来。如果一下代码有错误或者有bug还请路过的大神指点指点,这些方法对你们有帮助请给个支持,有其他的好方法也请留言哦。

     1 /**
     2  *  获取几天之前日期
     3  */
     4  daysAgo(dayNum = 0) {
     5     let myDate = new Date()
     6     let lw = new Date(myDate - 1000 * 60 * 60 * 24 * dayNum) // 最后一个数字多少天前的意思
     7     let lastY = lw.getFullYear()
     8     let lastM = lw.getMonth() + 1
     9     let lastD = lw.getDate()
    10     let startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD)
    11     return startdate
    12 }
     1 /**
     2  *  获取几个月之前日期
     3  */
     4  monthAgo(monthNum = 0) {
     5     let newDate = new Date();
     6     let year = newDate.getFullYear(); //获取当前日期的年份
     7     let month = newDate.getMonth() + 1; //获取当前日期的月份
     8     let day = newDate.getDate(); //获取当前日期的日
     9     let days = new Date(year, month, 0)
    10     days = days.getDate() //获取当前日期中月的天数
    11     let year2 = year
    12 
    13     // 进行月份计算
    14     let month2 = parseInt(month) - monthNum
    15     if (month2 <= 0) {
    16         year2 = parseInt(year2) - parseInt(month2 / 12 == 0 ? 1 : Math.abs(parseInt(month2 / 12)) + 1)
    17         month2 = 12 - (Math.abs(month2) % 12)
    18     }
    19 
    20     // 进行日期计算
    21     let day2 = day
    22     let days2 = new Date(year2, month2, 0)
    23     days2 = days2.getDate()
    24     if (day2 > days2) {
    25         day2 = days2
    26     }
    27 
    28     let startdate = year2 + "-" + (month2 < 10 ? "0" + month2 : month2) + "-" + (day2 < 10 ? "0" + day2 : day2)
    29     return startdate
    30 }
     1 /**
     2  *  获取几年之前日期
     3  */
     4  yearAgo(yearNum = 0) {
     5     let newDate = new Date();
     6     let year = newDate.getFullYear(); //获取当前日期的年份
     7     let month = newDate.getMonth() + 1; //获取当前日期的月份
     8     let day = newDate.getDate(); //获取当前日期的日
     9 
    10     let year2 = year - yearNum;
    11 
    12     let startdate = year2 + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day)
    13     return startdate
    14 }
     1 /**
     2    *  根据日期获取上一个月
     3    */
     4   preMonth(data) {
     5     let curMonth = new Date(data)
     6     let month = curMonth.getMonth() - 1;
     7     curMonth.setMonth(month);
     8     let newMonth = curMonth.getMonth() + 1;
     9     if (newMonth < 10) {
    10       newMonth = "0" + newMonth;
    11     }
    12     return curMonth.getFullYear() + '-' + newMonth;
    13   }
     1 /**
     2    *  获取当前年月(yyyy-MM)
     3    */
     4   theCurrentMonth() {
     5     let date = new Date();
     6     let nowMonth = date.getMonth() + 1;
     7     if (nowMonth >= 1 && nowMonth <= 9) {
     8       nowMonth = "0" + nowMonth;
     9     }
    10     let nowDate = date.getFullYear() + '-' + nowMonth;
    11     return nowDate
    12   }
     1 /**
     2    *  获取上个月第一天
     3    */
     4   firstdate() {
     5     let date = new Date();
     6     let nowMonth = date.getMonth() + 1;
     7     if (nowMonth >= 1 && nowMonth <= 9) {
     8       nowMonth = "0" + nowMonth;
     9     }
    10     let nowDate = date.getFullYear() + '-' + nowMonth + '-01';
    11     return nowDate
    12   }
     1 /**
     2    *  获取上个月最后一天
     3    */
     4   enddate() {
     5     let date = new Date();
     6     let day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
     7     let nowMonth = date.getMonth() + 1;
     8     if (nowMonth >= 1 && nowMonth <= 9) {
     9       nowMonth = "0" + nowMonth;
    10     }
    11     let nowDate = date.getFullYear() + '-' + nowMonth + '-' + day;
    12 
    13     return nowDate
    14   }
  • 相关阅读:
    mysql_wp_replication_tutorial
    Procedure execution failed 2013
    [Err] 1136
    innodb_flush_log_at_trx_commit和sync_binlog 参数说明
    mysql没有oracle 那样一次性把data buffer 数据写入磁盘
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field
    MyEclipse之Widget is disposed
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
    An internal error occurred during: "Building workspace". GC overhead limit exceeded
    Oracle 内部复制文档解读
  • 原文地址:https://www.cnblogs.com/zhuyujie/p/13524398.html
Copyright © 2011-2022 走看看