当我使用 UTC() 函数获取纪元时间(1970 年 1 月 1 日)的总秒数时。但再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC date 。 (按照 UTC 时间作为第二个 aare)。
我在印度(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()”给出 UTC 日期字符串的时间,2012 年 1 月 24 日星期二 14:02:17 GMT,比当前时间少 5:30。
提前致谢!
最佳答案
如果您在写入日期字符串时未指定 UTC,您将获得本地时间的字符串。
alert("date is "+new Date(sc).toUTCString());
关于javascript - 当使用这些 UTC 秒再次转换为日期时,当前日期的 UTC 秒数应给出 UTC 日期。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988153/