将毫秒转换为 AM 和 PM 日期格式
convert milliseconds to AM and PM date format
我正在将 TIMESTAMP
存储在数据库中,当我从数据库中取回它时,我想将其转换为上午和下午日期格式。
var dbDate = moment(milliseconds); // **i am getting an error over here**
var data = dbDate.format("hh:mm:A").split(":");
但我收到以下错误 moment(milliseconds);
"Deprecation warning: moment construction falls back to js Date. This
is discouraged and will be removed in upcoming major release. Please
refer to https://github.com/moment/moment/issues/1407 for more info.
我对 moment.js 不是很熟悉,但通过快速浏览文档,我认为构造函数不支持毫秒参数。试试这个:
var dbDate = moment({milliseconds: milliseconds});
编辑
上面的代码可能只是将其视为秒中小数点后的值 - 如果是这样,请尝试以下操作:
var dbDate = moment(new Date(milliseconds));
默认情况下,moment 库在构造函数中仅支持有限数量的格式。如果您不使用其中一种格式,它将默认返回使用 new Date
,其中 browsers are free to interpret the given date how they choose 除非它符合特定条件。
弃用警告是为了警告您这种行为 - 它不一定是错误。
在你的例子中,你有毫秒数,所以你可以使用 moment constructor that has a format parameter,告诉它你具体传递的是毫秒数:
var dbDate = moment(milliseconds, 'x');
所有这一切都假设您当前有 milliseconds
作为字符串 returned 到您的 JavaScript 层。如果你 return 它并将其视为一个数字,你不应该看到那个警告,因为 moment 也有一个 specific constructor that takes a single Number,如果你的 milliseconds
参数是一个数字应该是已经用过了。
我正在将 TIMESTAMP
存储在数据库中,当我从数据库中取回它时,我想将其转换为上午和下午日期格式。
var dbDate = moment(milliseconds); // **i am getting an error over here**
var data = dbDate.format("hh:mm:A").split(":");
但我收到以下错误 moment(milliseconds);
"Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
我对 moment.js 不是很熟悉,但通过快速浏览文档,我认为构造函数不支持毫秒参数。试试这个:
var dbDate = moment({milliseconds: milliseconds});
编辑
上面的代码可能只是将其视为秒中小数点后的值 - 如果是这样,请尝试以下操作:
var dbDate = moment(new Date(milliseconds));
默认情况下,moment 库在构造函数中仅支持有限数量的格式。如果您不使用其中一种格式,它将默认返回使用 new Date
,其中 browsers are free to interpret the given date how they choose 除非它符合特定条件。
弃用警告是为了警告您这种行为 - 它不一定是错误。
在你的例子中,你有毫秒数,所以你可以使用 moment constructor that has a format parameter,告诉它你具体传递的是毫秒数:
var dbDate = moment(milliseconds, 'x');
所有这一切都假设您当前有 milliseconds
作为字符串 returned 到您的 JavaScript 层。如果你 return 它并将其视为一个数字,你不应该看到那个警告,因为 moment 也有一个 specific constructor that takes a single Number,如果你的 milliseconds
参数是一个数字应该是已经用过了。