Date.UTC() returns 上个月的最后一天 while getUTCFullYear(), getUTCMonth() returns 正确月份的第一天
Date.UTC() returns last day of previous month while getUTCFullYear(), getUTCMonth() returns correctly first day of correct month
我得到了一个奇怪的结果:
var date = new Date();
var year = date.getMonth() < 11 ? date.getFullYear() : date.getFullYear() + 1;
var month = date.getMonth() < 11 ? date.getMonth() + 1 : 0;
console.log(new Date(Date.UTC(year, month)));
var utcYear = date.getUTCMonth() < 11 ? date.getUTCFullYear() : date.getUTCFullYear() + 1;
var utcMonth = date.getUTCMonth() < 11 ? date.getUTCMonth() + 1 : 0;
console.log(new Date(utcYear, utcMonth));
对于我使用的特定日期(任何日期都可以),Date.UTC 给我:
2015 年 5 月 31 日星期日 19:00:00 GMT-0500(中部夏令时)
getUTC... 方法给我:
2015 年 6 月 1 日星期一 00:00:00 GMT-0500(中部夏令时)
我是在误用 Date.UTC 还是遗漏了什么?
谢谢
您正在使用 UTC 时间创建一个日期,但随后您以当地时间显示它,这就是它晚几个小时的原因。使用Date.prototype.toUTCString()查看UTC时间
var date = new Date();
var year = date.getMonth() < 11 ? date.getFullYear() : date.getFullYear() + 1;
var month = date.getMonth() < 11 ? date.getMonth() + 1 : 0;
// It's a UTC date, display it as UTC, not local time
console.log(new Date(Date.UTC(year, month)).toUTCString());
var utcYear = date.getUTCMonth() < 11 ? date.getUTCFullYear() : date.getUTCFullYear() + 1;
var utcMonth = date.getUTCMonth() < 11 ? date.getUTCMonth() + 1 : 0;
// Created using local time, you can just use the normal toString()
console.log(new Date(utcYear, utcMonth));
This code is to get the first day of the next month (something that reoccurs monthly and can't start until next month).
那么您正在寻找
var today = new Date(); // now
var utcYear = today.getUTCMonth() < 11 ? today.getUTCFullYear() : today.getUTCFullYear() + 1;
var utcMonth = today.getUTCMonth() < 11 ? today.getUTCMonth() + 1 : 0;
var date = new Date(Date.UTC(utcYear, utcMonth)); // next UTC month
console.log(date.toString()); // Mon Jun 01 2015 02:00:00 GMT+0200
// or, in your timezone: Sun May 31 2015 19:00:00 GMT-0500 (same moment)
console.log(date.toUTCString()); // Mon, 01 Jun 2015 00:00:00 GMT
顺便说一句,Date
方法确实会将太大的值计入帐户(并自动结转),因此您只需要执行
var today = new Date(); // now
var date = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth()+1)); // next UTC month
我得到了一个奇怪的结果:
var date = new Date();
var year = date.getMonth() < 11 ? date.getFullYear() : date.getFullYear() + 1;
var month = date.getMonth() < 11 ? date.getMonth() + 1 : 0;
console.log(new Date(Date.UTC(year, month)));
var utcYear = date.getUTCMonth() < 11 ? date.getUTCFullYear() : date.getUTCFullYear() + 1;
var utcMonth = date.getUTCMonth() < 11 ? date.getUTCMonth() + 1 : 0;
console.log(new Date(utcYear, utcMonth));
对于我使用的特定日期(任何日期都可以),Date.UTC 给我:
2015 年 5 月 31 日星期日 19:00:00 GMT-0500(中部夏令时)
getUTC... 方法给我:
2015 年 6 月 1 日星期一 00:00:00 GMT-0500(中部夏令时)
我是在误用 Date.UTC 还是遗漏了什么?
谢谢
您正在使用 UTC 时间创建一个日期,但随后您以当地时间显示它,这就是它晚几个小时的原因。使用Date.prototype.toUTCString()查看UTC时间
var date = new Date();
var year = date.getMonth() < 11 ? date.getFullYear() : date.getFullYear() + 1;
var month = date.getMonth() < 11 ? date.getMonth() + 1 : 0;
// It's a UTC date, display it as UTC, not local time
console.log(new Date(Date.UTC(year, month)).toUTCString());
var utcYear = date.getUTCMonth() < 11 ? date.getUTCFullYear() : date.getUTCFullYear() + 1;
var utcMonth = date.getUTCMonth() < 11 ? date.getUTCMonth() + 1 : 0;
// Created using local time, you can just use the normal toString()
console.log(new Date(utcYear, utcMonth));
This code is to get the first day of the next month (something that reoccurs monthly and can't start until next month).
那么您正在寻找
var today = new Date(); // now
var utcYear = today.getUTCMonth() < 11 ? today.getUTCFullYear() : today.getUTCFullYear() + 1;
var utcMonth = today.getUTCMonth() < 11 ? today.getUTCMonth() + 1 : 0;
var date = new Date(Date.UTC(utcYear, utcMonth)); // next UTC month
console.log(date.toString()); // Mon Jun 01 2015 02:00:00 GMT+0200
// or, in your timezone: Sun May 31 2015 19:00:00 GMT-0500 (same moment)
console.log(date.toUTCString()); // Mon, 01 Jun 2015 00:00:00 GMT
顺便说一句,Date
方法确实会将太大的值计入帐户(并自动结转),因此您只需要执行
var today = new Date(); // now
var date = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth()+1)); // next UTC month