片刻没有找到时间

时间:2016-06-02 15:22:13

标签: javascript angularjs momentjs

我正在使用angularJs和moment js。我有一个发送例如

的文本框
  

中午12:00

到函数(apptime) - 然后我做

 var newtime = moment(apptime,'hh:mm:ss');

这就是控制台输出的内容:

Thu Jan 01 1970 12:00:00 GMT+0000 (GMT Standard Time)

所以时间很好,我看到那里的12:00:00 ..

然后(因为数据库结构)我想将它转换为:

1899-12-30 12:00:00

所以我这样做:

console.log("1899-12-30 " + newtime.format('hh:mm:ss'));

无论日期是什么,响应总是

  

1899-12-30 01:00:00

为什么认为01是小时?

2 个答案:

答案 0 :(得分:1)

它应该转换为UTC时间。

变化:

console.log("1899-12-30 " + newtime.format('hh:mm:ss'));

要:

console.log("1899-12-30 " + moment(newtime).utc().format('hh:mm:ss'));

答案 1 :(得分:0)

试试这个。您对moment.js的输入格式不正确。

var apptime = '12:00 PM';
var newtime = moment(apptime,'hh:mm a');
console.log(apptime);
console.log(newtime);
console.log("1899-12-30 " + newtime.format('HH:mm:ss'));

我还将输出格式修改为军事时间,但如果您愿意,可以将其修改为基本上午/下午。

相关问题