Javascript - 同一客户端上的不同时间戳值

时间:2018-06-16 18:40:33

标签: javascript datetime timezone timestamp

有人可以解释一下为什么这3条不同的行会产生完全相同的结果,给出三种不同的结果?

唯一准确的是2行( Date.now())。不幸的是,这是我唯一不能使用的。

function show_ts_date(idx, ts)
{
     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 ;

     alert(idx+'. timestamp: '+ts+' Date: '+formattedTime);
}

var currentdate = new Date(); 

//line 1: (produces wrong timestamp - it gives the wrong hour (-4) when I convert back to dateTime)
timestamp_1 = Math.floor(new Date(currentdate.getFullYear()+'-'+(currentdate.getMonth()+1)+'-'+currentdate.getDate()+'T'+currentdate.getHours()+':'+currentdate.getMinutes()+':00') / 1000); 
show_ts_date(1, timestamp_1);


//line 2 (produces correct timastamp - it gives the correct hour when I convert back to DateTime.)
timestamp_2 = Math.floor(Date.now() / 1000); 
show_ts_date(2, timestamp_2);


//line 3 (produces wrong timestamp - it gives the wrong hour (+3) when I convert back to dateTime)
let dat = new Date(Date.UTC(currentdate.getFullYear(), currentdate.getMonth(), currentdate.getDate(), currentdate.getHours(), currentdate.getMinutes(), 00));
timestamp_4 = Math.floor( dat/ 1000);
show_ts_date(4, timestamp_4);

1 个答案:

答案 0 :(得分:0)

好吧,假设Date.now()返回真正的准确值(我怀疑它总是如此,但那就是我剩下的。你总是可以将它转换回Date并检查是否有正确的日期和放大器;时间回来了), 我编写了这个函数,它将在正确和错误的时间戳之间进行比较,并将添加(或减少)从错误时间戳开始的毫秒数 - 将其转换为正确的时间戳:

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...