Javascript 日期 UTC 问题
Javascript Date UTC issue
我在这里尝试将 日期转换为 UTC,然后将相同的 UTC 日期 转换为新日期。但问题是我没有得到我通过的相同日期。这是我的 fiddle
我在这里传递日期 11/2/2015 即 2 November 2015,然后将其转换为 UTC 格式,然后使用 new Date 将 UTC 转换为正常日期 但现在它在 2015 年 11 月 1 日返回我,即 2015 年 11 月 1 日。
提前致谢。
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDay());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
getDay()
方法不会 return 月份的日期在 js Date.getDay() 处查看更多
因此,您需要通过 getDate()
获得的月份日期,而不是在 getDate()
处查看更多信息
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDate());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
我用过
myDateObj.getDate()
它工作正常,请立即检查。
var myDate = '11/02/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
console.log('my Date is \n' + myDateObj.getDate());
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDate());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
发生这种情况是因为它首先将日期转换为以毫秒为单位的长值,然后转换为日期,因此 11 月 2 日 00:00:00 它可以作为 11 月 1 日 23:30:00 因此您也可以在日期如下:
var myDate = '11/2/2015 23:00:00';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDay(),myDateObj.getHours(),myDateObj.getMinutes(),myDateObj.getSeconds());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
Here I am trying to convert a date to UTC and then converting same UTC date as new date. But the issue is that I am not getting same date which I passed.
所有 Date 对象都使用单个 UTC 时间值,因此它们本质上都是 UTC,无需 "convert" 将它们转换为 UTC。
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
您不应使用 Date 构造函数(或 Date.parse)来解析字符串、编写小函数或使用库(例如 moment.js, fecha.js)。
以下(将 getDay 替换为 getDate):
var tempDate = Date.UTC(myDateObj.getFullYear(),
myDateObj.getMonth(),
myDateObj.getDate());
只是复制日期的冗长方法。使用起来更简单:
var tempDate = new Date(myDateObj);
如果要获取UTC日期值,使用UTC方法如myDateObj.getUTCHours,myDateObj.getUTCMinutes等
如果要获取 UTC 日期和时间,请使用 toISOString 其中 returns 偏移量为 0000 的值:
// Create a Date for 2 November, 2016
var date = new Date(2016,10,2);
// Copy it
var dateCopy = new Date(date);
// Show local date and time
console.log('Local date and time:\n' + dateCopy.toString());
// Show UTC date and time
console.log('UTC date and time :\n' + dateCopy.toISOString());
// Show UTC Date only
console.log('UTC date only :\n' + dateCopy.toISOString().substr(0,10));
我在这里尝试将 日期转换为 UTC,然后将相同的 UTC 日期 转换为新日期。但问题是我没有得到我通过的相同日期。这是我的 fiddle
我在这里传递日期 11/2/2015 即 2 November 2015,然后将其转换为 UTC 格式,然后使用 new Date 将 UTC 转换为正常日期 但现在它在 2015 年 11 月 1 日返回我,即 2015 年 11 月 1 日。
提前致谢。
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDay());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
getDay()
方法不会 return 月份的日期在 js Date.getDay() 处查看更多
因此,您需要通过 getDate()
获得的月份日期,而不是在 getDate()
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDate());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
我用过
myDateObj.getDate()
它工作正常,请立即检查。
var myDate = '11/02/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
console.log('my Date is \n' + myDateObj.getDate());
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDate());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
发生这种情况是因为它首先将日期转换为以毫秒为单位的长值,然后转换为日期,因此 11 月 2 日 00:00:00 它可以作为 11 月 1 日 23:30:00 因此您也可以在日期如下:
var myDate = '11/2/2015 23:00:00';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
var tempDate = Date.UTC(myDateObj.getFullYear(),myDateObj.getMonth(),myDateObj.getDay(),myDateObj.getHours(),myDateObj.getMinutes(),myDateObj.getSeconds());
var utcDate = new Date(tempDate);
console.log('my Date after utc \n' + utcDate);
Here I am trying to convert a date to UTC and then converting same UTC date as new date. But the issue is that I am not getting same date which I passed.
所有 Date 对象都使用单个 UTC 时间值,因此它们本质上都是 UTC,无需 "convert" 将它们转换为 UTC。
var myDate = '11/2/2015';
var myDateObj = new Date(myDate);
console.log('my Date is \n' + myDateObj);
您不应使用 Date 构造函数(或 Date.parse)来解析字符串、编写小函数或使用库(例如 moment.js, fecha.js)。
以下(将 getDay 替换为 getDate):
var tempDate = Date.UTC(myDateObj.getFullYear(),
myDateObj.getMonth(),
myDateObj.getDate());
只是复制日期的冗长方法。使用起来更简单:
var tempDate = new Date(myDateObj);
如果要获取UTC日期值,使用UTC方法如myDateObj.getUTCHours,myDateObj.getUTCMinutes等
如果要获取 UTC 日期和时间,请使用 toISOString 其中 returns 偏移量为 0000 的值:
// Create a Date for 2 November, 2016
var date = new Date(2016,10,2);
// Copy it
var dateCopy = new Date(date);
// Show local date and time
console.log('Local date and time:\n' + dateCopy.toString());
// Show UTC date and time
console.log('UTC date and time :\n' + dateCopy.toISOString());
// Show UTC Date only
console.log('UTC date only :\n' + dateCopy.toISOString().substr(0,10));