为什么这会返回 31st 而不是 1st ?据我所知,UTC
方法需要3个参数(全年,月,日),并且day参数必须是1到31之间的整数。因为getDate()
返回0到31之间的整数,我也怀疑0
是可能的。
firstDay = new Date(Date.UTC( 2011 , 7 , 1 )).getDate();
// returns 31 (last day of this month)
让我澄清并说,这不是一个特例。使用day参数为2,3或4,这将返回1,2,3等等。
答案 0 :(得分:2)
你的时区偏移是负数,所以像-4。所以2011年7月1日上午12:00减去4小时是2011年6月31日晚上8点。 Date.UTC会使用其他参数来传递小时,分钟,秒和毫秒。
但实际上,如果您不想使用new Date(year, month, day)
firstDay = new Date(2011 , 7 , 1).getDate(); // returns 1 (first day of this month)
答案 1 :(得分:1)
我在(GMT-0700) Pacific Time
。以下是我执行以下操作时的结果:
new Date( 2011, 7, 1 );
// -> Mon Aug 01 2011 00:00:00 GMT-0700 (Pacific Daylight Time)
new Date( Date.UTC( 2011, 7, 1 ) );
// -> Sun Jul 31 2011 17:00:00 GMT-0700 (Pacific Daylight Time)
请注意,拉动UTC时间会在指定日期前7小时提供当前位置的日期/时间,因为我比格林威治标准时间落后7小时。
答案 2 :(得分:0)
这就是你要找的东西:
new Date(Date.UTC(2011, 7, 1) + ((new Date).getTimezoneOffset() / 60) * 3600000).getDate();
说明:
(new Date).getTimeZoneOffset(); // will retrieve the timezone offset, (420 in my case PST)
offset / 60; // we divide by 60 so we can get the number of hours between UTC and me
(offset / 60) * 360000 // is 60 (seconds) * 60 (minutes) * 1000 (ms)
+ Date.UTC(2011, 7, 1) // will give us the correct Date always