如何动态生成历史时间数据用于测试?
-
场景:
- 在开发或测试环境中,可能需要模拟几年前的数据。
-
代码示例(JavaScript 动态生成时间):
javascriptfunction generatePastDate(yearsAgo) { const now = new Date(); const pastDate = new Date(now.getFullYear() - yearsAgo, now.getMonth(), now.getDate()); return pastDate.toISOString().split('T')[0]; // 返回格式:YYYY-MM-DD } console.log("5年前的日期:", generatePastDate(5)); -
应用场景:
- 测试网站的时间显示逻辑。
- 验证数据库查询条件是否能正确筛选出几年前的数据。

更新时间:2025-12-06 16:28:03
上一篇:如何批量修改多篇文章的发布时间?
