为什么我的jquery new Date()函数会覆盖传入的参数?

why does my jquery new Date() function overwrite the parameter passed in?

本文关键字:覆盖 参数 函数 jquery 我的 new Date 为什么      更新时间:2023-09-26

我有以下php代码,返回不同时区的时间:

<?php
date_default_timezone_set('America/New_York'); 
echo (new DateTime())->format('r');
?>

这很好,当我在浏览器中运行它时,我得到时间:

Tue, 29 Sep 2015 12:07:01 -0400
现在,我有一个jquery脚本调用ajax这个php页面,并显示在我的页面上。它是这样开始的:
$.ajax({
    type: 'GET', 
    url: 'timeinnewyork.php',
    complete: function(resp){
        var today;
        today = new Date(resp.responseText);
        alert(resp.responseText);
        alert(today);
        (...)

和第一个警报返回纽约的时间:

Tue, 29 Sep 2015 12:07:27 -0400

但是第二个显示的是我当前的欧洲时区:

Tue Sep 29 2015 18:07:27 GMT+0200 (Central Europe Daylight Time)

我想用纽约时间而不是我的当地时间。我怎样才能解决这个问题呢?

从PHP返回UTC时间,然后使用http://momentjs.com/timezone/

等库在JS中格式化