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

如何让JavaScript Date对象返回两位格式的月份与日期?

Fixing Single-Digit Month/Date Formatting in JavaScript Dates

Hey there! I see the issue you're facing—your current code doesn't add leading zeros to single-digit months and dates, and your initial if approach only handled January, which is why it didn't work for other months. Let's fix this properly.

The Core Problem

The getMonth() + 1 and getDate() methods return numeric values (1-12 for months, 1-31 for dates). When these values are less than 10, they show up as single digits instead of two (e.g., 5 instead of 05). Your original if(month == 1) only targeted January, so months 2-9 and dates 1-9 still had the same problem.

Solution 1: Create a Reusable Padding Function

A simple utility function to add leading zeros will work for both months and dates. Here's how to implement it:

// Utility function to add leading zero to numbers < 10
function padZero(num) {
  return num < 10 ? `0${num}` : num;
}

var d = new Date();
var month = padZero(d.getMonth() + 1); // Now returns "01" to "12"
var year = d.getFullYear();
var day = padZero(d.getDate()); // Now returns "01" to "31"
var highestDate = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
var fromDateString = `${year}-${month}-01`; // Using template literals for cleaner syntax
var toDateString = `${year}-${month}-${day}`;

console.log(`${fromDateString} - ${toDateString}`);

Why This Works:

  • The padZero function checks if the number is less than 10. If yes, it prepends a 0 and returns it as a string; otherwise, it returns the original number.
  • We use template literals (backticks `) instead of string concatenation with +—this makes the code easier to read and maintain.

Solution 2: Use ES6's padStart() Method

If you're working in an environment that supports ES6 (most modern browsers and Node.js), you can use the built-in padStart() method for strings:

var d = new Date();
var month = String(d.getMonth() + 1).padStart(2, '0');
var year = d.getFullYear();
var day = String(d.getDate()).padStart(2, '0');
var highestDate = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
var fromDateString = `${year}-${month}-01`;
var toDateString = `${year}-${month}-${day}`;

console.log(`${fromDateString} - ${toDateString}`);

How padStart() Works:

  • padStart(2, '0') ensures the string is at least 2 characters long. If it's shorter, it adds 0s to the start until it reaches 2 characters.
  • We first convert the numeric month/day to a string with String() because padStart() only works on strings.

Why Your Original if Didn't Work

Your initial code only checked if month == 1—this would only add a zero to January. For months 2 through 9, it left them as single digits. You'd have needed to check month < 10 instead, but using a reusable function or padStart() is much cleaner.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:05:00