You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript获取次日当月日期?求类似getDate()的实现方法

Get Next Day's Numeric Date in JavaScript (Like getDate())

Hey there! I totally get what you're after—you want a way to grab the next day's date as a plain numeric value, just like how getDate() spits out the current day of the month. The good news is JavaScript's built-in Date object handles this automatically, no need to manually calculate month lengths or leap years!

Here's a simple, reliable implementation:

// Start with today's date (swap this with your target date if needed)
const currentDate = new Date();

// Create a copy of the date to avoid messing with the original
const nextDay = new Date(currentDate);

// Add 1 day—this automatically handles month/year rollovers!
nextDay.setDate(currentDate.getDate() + 1);

// Get the numeric day of the month for the next day
const nextDayNumeric = nextDay.getDate();

console.log(nextDayNumeric); // Example outputs: 5 if today is the 4th, 1 if today is the last day of the month

How it works:

  • The setDate() method is the magic here. If you pass a number larger than the current month's total days, it automatically increments the month (and year if needed) and sets the date to the corresponding day of the next month.
  • For example:
    • If today is January 31st, setDate(31 + 1) will set the date to February 1st, and getDate() returns 1.
    • If today is December 31st, it rolls over to January 1st of the next year, with getDate() returning 1.

Want to use a specific date instead of today?

Just replace currentDate with your target date (remember: months are 0-indexed in JavaScript!):

// Let's use February 29, 2024 (a leap year)
const specificDate = new Date(2024, 1, 29);
const nextDay = new Date(specificDate);
nextDay.setDate(specificDate.getDate() + 1);
console.log(nextDay.getDate()); // Outputs 1 (March 1st)

This approach gives you exactly the numeric value you need, just like getDate(), and takes care of all edge cases automatically.

内容的提问来源于stack exchange,提问作者Luke Strong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:03:00