Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript time.utc(date).local()仍然提供utc时间。_Javascript_Datetime_Momentjs - Fatal编程技术网

Javascript time.utc(date).local()仍然提供utc时间。

Javascript time.utc(date).local()仍然提供utc时间。,javascript,datetime,momentjs,Javascript,Datetime,Momentjs,我正在从数据库中获取2017-06-28 23:24:17.1-07格式的时间戳,并尝试使用矩将其转换为本地时间,但我仍在获取UTC时间 这是我插入控制台的模拟版本: var now = new Date(); // Fri Jun 30 2017 15:45:30 GMT-0700 (PDT) // simulate date received from server var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth()

我正在从数据库中获取2017-06-28 23:24:17.1-07格式的时间戳,并尝试使用矩将其转换为本地时间,但我仍在获取UTC时间

这是我插入控制台的模拟版本:

var now = new Date(); // Fri Jun 30 2017 15:45:30 GMT-0700 (PDT)

// simulate date received from server
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); // Fri Jun 30 2017 22:45:41 GMT-0700 (PDT);

var local = moment.utc(now_utc).local().format('LLL') // "June 30, 2017 10:45 PM"

^ should be ~3:45pm.
如您所见,local的输出与now_utc的输入相同

我正在从数据库中获取一个2017-06-28 23:24:17.1-07格式的时间戳,我正在尝试使用矩将其转换为本地时间

这是一种非常不寻常的字符串格式,可以直接从数据库中获取,但如果这确实是您拥有的字符串,那么:

// parse the input string
var m = moment("2017-06-28 23:24:17.1-07", "YYYY-MM-DD HH:mm:ss.SZ");

// format the output string
var s = m.format("LLL");

根本不需要使用Date对象,也不需要调用.local,因为本地模式是默认模式,您提供的是偏移量。

我也有同样的问题。使用“瞬间时区”解决了我的问题,尽管我不明白为什么。本地不起作用

尝试按如下方式修改局部变量:

var local=moment.utcnow\u utc.tzTimeZone.format'LLL'

您可以找到所需的时区值


注意:别忘了安装矩时区软件包。

您是否指定了时区参数?