【问题标题】:UTC seconds for the current date should give the UTC date when it is again converted to Date using these UTC seconds .当前日期的 UTC 秒数应在使用这些 UTC 秒数再次转换为 Date 时给出 UTC 日期。
【发布时间】:2012-02-17 18:39:48
【问题描述】:

当我使用 UTC() 函数从纪元时间 (1 jan 1970) 获取总秒数时。但是再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC date 。 (按照 UTC 时间的秒数)。

我在印度 (GMT +05:30 ) 以下是我的 Html 文件

<html>
 <head>
   <title>GMT Calculator</title>
   <script type="text/javascript">
     function calcGMT(form) {
     var dt = new Date(); 
     var sc =Date.UTC(dt.getFullYear(),dt.getMonth(),dt.getDate(),dt.getHours(),dt.getMinutes(),dt.getSeconds()) ;
alert("date is "+new Date(sc));
}

window.onload = calcGMT;
</script>
</head>
<body>
</body>
</html>

我还发现它给出的一件事是“日期是 2012 年 1 月 25 日星期三 01:02:17 GMT+0530(印度标准时间)”,时间为 19:32,比当前时间早 5:30 .我不明白发生了什么。它应该根据函数 "toUTCString()" Tue, 24 Jan 2012 14:02:17 GMT 给出 UTC 日期字符串的时间,比当前时间少 5:30。

提前致谢!!

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    如果你在写日期字符串时没有指定UTC,你会得到本地时间的字符串。

    alert("date is "+new Date(sc).toUTCString());
    

    【讨论】:

    • 但是这个 Date 构造函数为您提供了从 1970 年 1 月 1 日开始的毫秒后的日期。因此,如果这些秒数现在是 UTC 日期的 UTC 秒数。那么当我们使用这个 Date 构造函数使用这个 seconds 时,这应该将这些秒隐蔽为确切的日期,即 UTC 日期。
    【解决方案2】:

    像往常一样,当有人为糟糕的 Date API 苦苦挣扎时,我推荐DateJS

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2012-11-17
      相关资源
      最近更新 更多