js 判断两个日期相距几天(yyyy-MM-dd)格式

1.主方法入口,其中获得今天日期方法在下面一个代码块

  getIsExpire = () => {
    const endDt = 20210807; //结束时间格式
    let day1 = new Date(this.getToDay()); //获取今天的日期
    //将结束时间yyyyMMdd转换为yyyy-MM-dd格式
    let day2 = new Date(endDt.slice(0,4) + "-" + endDt.slice(4,6) + "-" + endDt.slice(6,8));
    //计算相差几天 1000毫秒 = 1秒 60秒 = 1分 60分 = 1h 24 小时 = 1天
    const distanceTime = (day2 - day1) / (1000 * 60 * 60 * 24);
    //由于是在react 格式写在一周内的判断,所以是用原生还是vue自己转换一下
    if( distanceTime <= 7 ){
      this.setState({
        isExpire: true,
      });
    }else{
      this.setState({
        isExpire: false,
      });
    }
  }

2.获得当前日期方法(yyyy-MM-dd格式)

  getToDay() {
  	//生成今天的时间
    let now = new Date();
    //获得年份 例如 2021
    let nowYear = now.getFullYear();
    //获得月份
    let nowMonth = now.getMonth();
    //获得今天日期
    let nowDate = now.getDate();
    //这一步好像没必要
    let newdate = null;
    //这一步好像也没必要,但为了不报错,就先放在这
    newdate = new Date(nowYear, nowMonth, nowDate);
    //调用doHandleMonth判断当前字符是不是小于10,如果小于10,需要给前面加上一个0,例如07
    //这个月份+1是因为我们月份是从0开始的也就是6月份得到的是5所以需要+1
    nowMonth = this.doHandleMonth(nowMonth + 1);
    //日期和月份一样需要判断是否小于10
    nowDate = this.doHandleMonth(nowDate);
    //字符串拼接
    return nowYear + "-" + nowMonth + "-" + nowDate;
  }

3.判断月份或日期是否小于10

  doHandleMonth(month) {
    if (month.toString().length === 1) {
      month = "0" + month;
    }
    return month;
  }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下一个简单的日期处理类,满足题目要求: ```java import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { // 显示当前系统日期和时间 public static void showCurrentDateTime() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String dateTime = dateFormat.format(date); System.out.println(dateTime); } // 计算并显示若干天后的日期值 public static void showDateAfterDays(String currentDate, int days) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = dateFormat.parse(currentDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, days); Date newDate = calendar.getTime(); String newDateString = dateFormat.format(newDate); System.out.println(newDateString); } catch (ParseException e) { e.printStackTrace(); } } // 计算并显示两个日期之间相距的天数 public static void showDaysBetweenDates(String date1, String date2) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date startDate = dateFormat.parse(date1); Date endDate = dateFormat.parse(date2); long diff = endDate.getTime() - startDate.getTime(); long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println(diffDays); } catch (ParseException e) { e.printStackTrace(); } } } ``` 使用示例: ```java public static void main(String[] args) { // 显示当前系统日期和时间 DateUtils.showCurrentDateTime(); // 计算并显示若干天后的日期DateUtils.showDateAfterDays("2021-01-01", 10); // 计算并显示两个日期之间相距的天数 DateUtils.showDaysBetweenDates("2021-01-01", "2021-01-11"); } ``` 输出结果: ``` 2021-05-01 15:23:45 2021-01-11 10 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值