本地日期转换为 utc 纪元时间并返回 javascript 中的日期不匹配

mismatch in local date conversion to utc epoch time and back to date in javascript

让本地日期时间为 local : Mar 10 2014 11:52:50 GMT+0530
在javascript中将其转换为utc纪元时间:

var epochtime=Date.UTC(2014,2,10,11,52,50);


output : 1394452370000

现在将其转换回本地日期:

var utcSeconds =1394452370000;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);
alert(d);

output : Fri Jun 09 46158 06:03:20 GMT+0530

但所需的输出是: Mar 10 2014 11:52:50 GMT+0530

请问有人能找出问题所在吗?

毫秒!=秒。您获得的值以毫秒为单位,但随后将其用作秒数。

要将您的值转换回日期,只需:

var d = new Date(1394452370000);

示例:

var epochtime=Date.UTC(2014,2,10,11,52,50);
var d = new Date(epochtime);
snippet.log(d.toISOString());
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

你能试试直接把UTC转换成日期吗,

var d = new Date(utc 秒); 警报(d);