我有一个JavaScript问题似乎无法解决:
我将Date转换为时间戳,当我将其转换回来时,它会显示正确的DateTime:
DateTime to Timestamp:
var ts = Math.floor(Date.now() / 1000);
和返回 - 时间戳到日期:
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);
和返回 - 时间戳到日期:
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小时:亚洲/耶路撒冷)
我做错了什么?
答案 0 :(得分:1)
(此答案遵循评论)
已经明确了Javascript Date会导致问题而OP选择了momentjs。
如果使用共享主机阻止您的客户端直接安装它,请将其包含在CDN中,例如https://cdnjs.com/libraries/moment.js/
答案 1 :(得分:0)
哈哈!!我应该寻找那个...哎呀。非常感谢你!
与此同时,我写了这个(假设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...