【问题标题】:Javascript - Convert DateTime to timestamp, then back to DateTimeJavascript - 将 DateTime 转换为时间戳,然后返回 DateTime
【发布时间】:2018-11-26 00:48:35
【问题描述】:

我有一个似乎无法解决的 JavaScript 问题:

我将日期转换为时间戳,当我将其转换回来时,它会显示正确的日期时间:

日期时间到时间戳:

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 小时:亚洲/耶路撒冷)

我做错了什么?

【问题讨论】:

  • UTC 和 GMT 时区显示我是 +3
  • 警报 (Math.floor(new Date('2018-06-19T15:40:00') / 1000)); //产生错误的时间戳和警报( Math.floor(Date.now() / 1000)); //产生正确的时间戳(你可以通过在第一行输入当前日期时间来测试它)
  • 试试:let dat = new Date(Date.UTC(2018, 5, 16, 15, 57, 00)) console.log(Math.floor( dat/ 1000)); console.log( Math.floor(Date.now() / 1000)); 注意六月是5,月份从0开始
  • Javascript 日期已知会导致问题。忘掉这个。试试momentjs.com
  • 实际上使用 CDN 可以让您的客户使用它。检查我的答案。 :)

标签: javascript datetime timestamp datetime-format intl


【解决方案1】:

(此答案遵循 cmets)

已明确 Javascript Date 已知会导致问题,并且 OP 选择了 momentjs。

如果使用共享主机阻止您的客户端直接安装它,请将其包含在 CDN 中,例如 https://cdnjs.com/libraries/moment.js/

【讨论】:

  • 我会用那个!非常感谢。
【解决方案2】:

哈哈!!我应该去找那个……哎呀。非常感谢!

与此同时,我写了这个(假设 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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-24
    • 2011-12-18
    • 2022-01-24
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多