javascript - Utilities.formatDate 使用 Array.map 返回错误的日期
问题描述
arr
我创建了一个函数,从 2013 年 1 月 1 日开始以 1 个月为增量生成日期数组,一直持续到现在。
function getDateRange() {
var start = new Date('1/1/2013');
var today = new Date();
var arr = [start];
var next = new Date(start);
while (next < today) {
arr.push(next);
next = new Date(next.setMonth(next.getMonth() + 1));
}
Logger.log(arr);
Logger.log(arr.map(formatDate));
}
function formatDate(d) {
return Utilities.formatDate(d, 'MST', 'MMM-dd');
}
该函数正确生成arr
,如下所示:
Jan 01 2013 00:00:00 GMT-0700 (MST),Fri Feb 01 2013 00:00:00 GMT-0700 (MST),Fri Mar 01 2013 00:00:00 GMT-0700 (MST),Mon Apr 01 2013 00:00:00 GMT-0600 (MDT),Wed May 01 2013 00:00:00 GMT-0600 (MDT)...
但是当我登录时arr.map(formatDate)
,从第 4 天开始,我没有得到相同的日期:
Jan-01,Feb-01,Mar-01,Mar-31,Apr-30...
任何想法为什么 Utilities.formatDate 搞砸了日期?
解决方案
function getDateRange() {
var start = new Date('1/1/2013');
var today = new Date();
var arr = [];
do {
arr.push(start);
start = new Date(start.setDate(start.getDate() + 1));
} while (start < today)
console.log(arr);
console.log(arr.map(formatDate));
}
function formatDate(date) {
return date.toLocaleString("en-us", {
month: "short",
timeZone: 'UTC'
}) + "-" + date.toLocaleString("en-us", {
day: "numeric",
timeZone: 'UTC'
});
}
推荐阅读
- java - 如何在 Java 中实现构建特定的注解保留
- php - 我无法在 Godaddy 服务器上使用 CURL
- amazon-web-services - DynamoDB 多对多等效
- excel - 如何在 VBA 中自动填充函数?
- xcode - Macos 应用程序在 Xcode 中显示视图,但在 Xcode 之外启动时完全空白
- docker - 如何在 Jenkins 全局工具配置中配置 Ansible?
- sql - 如何从主查询中的子查询中检索值?
- c++ - 我的 SFINAE 出了什么问题?:测试支持的运算符
- react-native - 无法弄清楚如何正确使用 KeyboardAvoidingView
- r - 我想在 ggplot2 的堆积条形图上显示数据值