JavaScript中如何将getTime时间戳添加到另一日期并转为yyyy-mm-dd格式
Got it, let's walk through how to tackle this step by step—super straightforward once you break it down:
Step 1: Get the Target Timestamp
First, you're already on the right track with grabbing the timestamp from your res[i].DATESTART value. Just a quick note: this line gives you the millisecond-level timestamp of that date, which is exactly what we need for date arithmetic:
// Grab the timestamp from your source date const timestampToAdd = new Date(res[i].DATESTART).getTime();
Pro tip: Always double-check that res[i].DATESTART is a valid date string (like '2024-06-15' or 'June 15, 2024'), otherwise new Date() will return an Invalid Date and getTime() will spit out NaN.
Step 2: Add the Timestamp to Another Date
Now, to add this timestamp to another date (say, a base date you've defined), follow these simple steps:
- Fetch the millisecond timestamp of your base date
- Add your target timestamp to it
- Create a new
Dateobject from the combined value
Example using the current date as the base:
// Define your base date (can be any fixed date too) const baseDate = new Date(); // Uses the current date/time // Combine the base date's timestamp with your target timestamp const combinedDate = new Date(baseDate.getTime() + timestampToAdd);
If you're using a fixed base date instead of the current time, just replace new Date() with something like new Date('2024-01-01').
Step 3: Format the Result to yyyy-mm-dd
Finally, we need to convert the combinedDate object into the clean yyyy-mm-dd string format. You have two solid options here:
Option 1: Manual Formatting (Simple & Explicit)
This gives you full control without relying on browser locale settings:
function formatDateToYMD(date) { const year = date.getFullYear(); // Add leading zeros to month/day (e.g., 06 instead of 6) const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } // Apply the function to your combined date const formattedDate = formatDateToYMD(combinedDate); console.log(formattedDate); // Outputs something like "2024-12-25"
Option 2: Using Intl.DateTimeFormat (Cleaner API Approach)
If you prefer leveraging built-in browser tools (and don't mind locale handling), this works great:
const formattedDate = new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }).format(combinedDate); // 'en-CA' locale uses yyyy-mm-dd format by default console.log(formattedDate); // Same yyyy-mm-dd output
Full Working Example
Putting it all together with sample data:
// Sample res[i].DATESTART value const sampleDateStart = '2024-06-15'; // Step 1: Get timestamp const timestampToAdd = new Date(sampleDateStart).getTime(); // Step 2: Add to base date (e.g., 2024-01-01) const baseDate = new Date('2024-01-01'); const combinedDate = new Date(baseDate.getTime() + timestampToAdd); // Step 3: Format function formatDateToYMD(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } const formattedDate = formatDateToYMD(combinedDate); console.log(formattedDate); // Output depends on combined timestamps!
内容的提问来源于stack exchange,提问作者M.SAM SIM

