Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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 如何在momentjs中添加本地时间的时间戳_Javascript_Node.js_Momentjs - Fatal编程技术网

Javascript 如何在momentjs中添加本地时间的时间戳

Javascript 如何在momentjs中添加本地时间的时间戳,javascript,node.js,momentjs,Javascript,Node.js,Momentjs,我使用的是moment.js,在数字/时间戳中有date和time。我想将其转换为本地时间戳,即GMT+5:30。这里是链接 代码:- console.log(moment(1489689000000).add(52200000).toDate()) //output: Fri Mar 17 2017 14:30:00 GMT+0530 (IST) //expected output : Fri Mar 17 2017 20:00:00 GMT+0530 (IST) 瞬间 如果你想在你

我使用的是
moment.js
,在数字/时间戳中有
date
time
。我想将其转换为本地时间戳,即
GMT+5:30
。这里是链接

代码:-

console.log(moment(1489689000000).add(52200000).toDate())

  //output: Fri Mar 17 2017 14:30:00 GMT+0530 (IST)
 //expected output : Fri Mar 17 2017 20:00:00 GMT+0530 (IST)
瞬间 如果你想在你的问题中使用普通的
moment
类似:

如果希望将日期解析为utc,可以尝试使用
矩.utc()

console.log(moment.utc(1489689000000).add(52200000).local().toDate());
或:

但是根据您的系统配置,这实际上可能无法满足您的需要

时刻时区 如果你对时区很认真,那么你应该使用
时刻时区

var moment = require('moment-timezone');
var zone = moment.tz.guess();
console.log(moment.utc(1489689000000).add(52200000).tz(zone).format());
见:


    • 这里是实现这一点的其他方法。根据您的
      GMT+5.30
      时区,您只需将小时转换为毫秒

      console.log(moment(1489689000000).add(52200000+ 19800000).toDate());
      
       // here 5.5 hours =  19800000 miliseconds
      // add  + 19800000 due GMT +5.30
      

      理想情况下,对于您的本地时区,矩对象将为您提供完美的日期

      moment().toDate() // Fri Mar 17 2017 20:18:53 GMT+0530 (IST)
      
      但若你们想要不同的时区,最好的方法就是转换


      不管怎样,moment.js都会拉本地时间,根据文档,试试moment().format('MMMM Do YYYY,h:mm:ss a');我认为您的代码也给出了2017年3月17日星期五14:30:00 GMT+0530(IST),但预期输出被称为2017年3月17日星期五20:00:00 GMT+0530(IST),而不是期望输出。。!实际上,当我从中选择“20:00”时,数字
      52200000
      来自前端
      bs-datepicker
      dropdown@user7104874请参阅我的更新答案-最后一个示例和链接。
      moment().toDate() // Fri Mar 17 2017 20:18:53 GMT+0530 (IST)
      
      moment().tz(timezone)toDate()
      eg. moment().tz('Asia/calcutta').toDate() // Fri Mar 17 2017 20:20:49 GMT+0530 (IST)