我有一个似乎无法解决的JavaScript问题:
我将日期转换为时间戳,当我将其转换回来时,它会显示正确的DateTime:
时间戳的DateTime:
var ts = Math.floor(Date.now() / 1000);
back - Timestamp to Date:
var a = new Date(ts * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var formattedTime = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
问题是,当我对输入数据执行此操作时,而不是使用Date.now(),它会转换为时间戳,但当我将其转换回dateTime时,Hour参数恰好提前了4小时。
var destinationDateTimeStr_ = document.getElementById("dateyear").value+"-"+document.getElementById("datemonth").value+"-"+document.getElementById("dateday").value+"T"+document.getElementById("datehour").value+":"+document.getElementById("dateminute").value+":00";
//convert date as string to timezone
var date2 = new Date(destinationDateTimeStr_); //2018-06-16T15:35:00
ts_ = Math.floor(date2 / 1000);
back - Timestamp to Date:
var a = new Date(ts_ * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var formattedTime = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
//returns: 2018-06-16T11:35:00 - 4 hours earlier
如果所有转换都在相同的客户端(相同的时区)上完成,这怎么可能?
然后我试着:
var offset = new Date().getTimezoneOffset();
但它返回-180,它只返回3小时,而不是4小时
然后我试着:
var tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
但它给了我一个文本(在我的例子中意味着-2小时:亚洲/耶路撒冷)
我做错了什么?
发布于 2018-06-17 01:15:03
(以下是评论之后的答案)
众所周知,Javascript日期会导致问题,而OP选择的是momentjs。
如果使用共享主机会阻止您的客户端直接安装,可将其与https://cdnjs.com/libraries/moment.js/等CDN一起安装
发布于 2018-06-17 04:43:37
哈哈!!我应该去找那个的..。哇哦。非常感谢!
同时,我写了这段代码(假设Date.now()总是返回真实的精确值,但事实可能并非如此):
function getTimestampMilisecondsGap()
{
var currentdate = new Date();
timestamp_1 = Math.floor(new Date(currentdate.getFullYear()+'-'+(currentdate.getMonth()+1)+'-'+currentdate.getDate()+'T'+currentdate.getHours()+':'+currentdate.getMinutes()+':00') / 1000);
//let dat = new Date(Date.UTC(currentdate.getFullYear(), currentdate.getMonth(), currentdate.getDate(), currentdate.getHours(), currentdate.getMinutes(), 00));
//timestamp_1 = Math.floor( dat/ 1000);
timestamp_2 = Math.floor(Date.now() / 1000); //this one is suppose to produce a correct timestamp
var addTimeStampMilisecs = 0;
if (timestamp_2 > timestamp_1)
{
addTimeStampMilisecs = timestamp_2-timestamp_1;
}
else if (timestamp_2 < timestamp_1)
{
addTimeStampMilisecs = timestamp_1-timestamp_2;
}
return addTimeStampMilisecs;
}
//writing a timestamp to the database
var destinationDateTimeStr = document.getElementById("dateyear").value+"-"+document.getElementById("datemonth").value+"-"+document.getElementById("dateday").value+"T"+document.getElementById("datehour").value+":"+document.getElementById("dateminute").value+":00";
var date2 = new Date(destinationDateTimeStr);
var eventDateTS = Math.floor(date2 / 1000); //convert to timestamp (with incorrect timezone)
eventDateTS += getTimestampMilisecondsGap(); //add (or decrese) the number of miliseconds from the timestamp because this function that generates the tmestamp returns a wrong number (the hour in the converted date is wrong)
//write the correct eventDateTS to your DB here...
https://stackoverflow.com/questions/50888021
复制相似问题