JS 获取下个月日期方法 或30天后时间

获取下一个月一号

const nextMonth = function () {
  let now = new Date();
  let month = now.getMonth()+1;
  let year  = now.getFullYear();
  if ( month === 12 ) {
    year += 1;
    month = '01';
  } else {
    month += 1;
    month = (month < 10) ? '0'+month : month;
  }

  return `${year}-${month}-01`
}

获取30天后时间


  var now = new Date()

  var seconds = 60*60*24*30*1000;

  var timestamp = now.getTime();

  var newDate = timestamp+seconds;

  console.log(new Date(newDate));

公众号:爱加菲猫Mike

对电影、音乐视频
感兴趣 微信公众号搜索: 爱加菲猫Mike

完~~~~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是用中文回答您的问题: 要创建一个Date对象,您可以使用JavaScript中的Date()构造函数。例如,下面的代码将创建一个Date对象,其日期为当前日期时间: ``` let currentDate = new Date(); ``` 要输出这个Date对象的日期,您可以使用Date对象的getDate()、getMonth()和getFullYear()方法。例如,下面的代码将输出当前日期的年、月和日: ``` console.log(currentDate.getFullYear()); // 输出当前年份 console.log(currentDate.getMonth() + 1); // 输出当前月份(注意要加1,因为getMonth()返回的是0~11) console.log(currentDate.getDate()); // 输出当前日期 ``` 要输出这个Date对象的下个月日期,您可以使用Date对象的setMonth()和getDate()方法。例如,下面的代码将输出下个月的年、月和日: ``` currentDate.setMonth(currentDate.getMonth() + 1); // 将月份设置为下个月 console.log(currentDate.getFullYear()); // 输出下个月的年份 console.log(currentDate.getMonth() + 1); // 输出下个月的月份 console.log(currentDate.getDate()); // 输出下个月的日期 ``` 需要注意的是,如果当前日期为一个月的最后一天,那么下个月的日期将会是下个月的第一天。 ### 回答2: 创建一个Date对象,可以通过以下方式: ``` let currentDate = new Date(); ``` 要输出当前日期,可以使用Date对象的`getDate()`、`getMonth()`和`getFullYear()`方法获取日期、月份和年份,并将它们连接起来: ``` let currentDate = new Date(); let day = currentDate.getDate(); let month = currentDate.getMonth() + 1; // 月份从0开始,所以要加1 let year = currentDate.getFullYear(); console.log("当前日期:" + year + "-" + month + "-" + day); ``` 要输出下个月的日期,可以使用Date对象的`setMonth()`方法设置月份,并使用`getDate()`方法获取日期: ``` let currentDate = new Date(); let nextMonth = currentDate.getMonth() + 1; currentDate.setMonth(nextMonth); // 设置为下个月 let nextMonthDay = currentDate.getDate(); let nextMonthYear = currentDate.getFullYear(); console.log("下个月日期:" + nextMonthYear + "-" + (nextMonth + 1) + "-" + nextMonthDay); ``` 以上代码将输出当前日期和下个月日期,每次运行输出结果会有所不同。 ### 回答3: 创建一个Date对象可以使用Date()函数。输出日期可以使用getDate()、getMonth()和getFullYear()等函数来获取年、月、日信息,并进行拼接成日期字符串进行输出。下个月的日期可以通过setMonth()函数来实现。 代码示例: ```javascript // 创建一个Date对象 var date = new Date(); // 获取当前日期信息 var year = date.getFullYear(); var month = date.getMonth() + 1; // 月份从0开始,需要加1 var day = date.getDate(); // 输出当前日期 console.log("当前日期:" + year + "年" + month + "月" + day + "日"); // 获取下个月的日期 date.setMonth(date.getMonth() + 1); // 获取下个月日期信息 var nextMonthYear = date.getFullYear(); var nextMonth = date.getMonth() + 1; var nextMonthDay = date.getDate(); // 输出下个月日期 console.log("下个月日期:" + nextMonthYear + "年" + nextMonth + "月" + nextMonthDay + "日"); ``` 执行上述代码后,会输出当前日期和下个月日期的信息,示例内容如下: ``` 当前日期:2022年11月30日 下个月日期:2022年12月30日 ``` 注意:以上示例中日期的输出可能因浏览器、操作系统、地区语言等不同而略有差异,但整体逻辑是通用的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值