简体   繁体   English

如何使用moment.js将秒传输到时间格式(YYYY:MM:DD HH:mm:ss)

[英]How to transfer seconds to Time format (YYYY:MM:DD HH:mm:ss) using moment.js

At first. 首先。

I using moment.js to get the diff time between two 'moment.js objects'. 我使用moment.js来获取两个'moment.js对象'之间的差异时间。

from is the beginning time ; from是开始时间; to is beginning time add the milliseconds. to开始时间加毫秒。

Variables 变量

var from = moment().format('YYYY-MM-DD HH:mm:ss');
var to   = moment().milliseconds(ms).format('YYYY-MM-DD HH:mm:ss');

Then I diff them. 然后我分享他们。

var difftime = moment(to).diff(from);

If from is '2015-01-01 00:00:00' and 'to' is '2015-01-02 00:00:00', I will get the difftime is '86400000' (Seems like milliseconds format). 如果from '2015-01-01 00:00:00'和'到'是'2015-01-02 00:00:00',我将得到difftime是'86400000'(看起来像毫秒格式)。

My problem. 我的问题。

How can I make difftime(86400000) transfer to YYYY:MM:DD HH:mm:ss(0000:00:01 00:00:00) using 'moment.js' . 如何使用'moment.js'difftime(86400000)传输到YYYY:MM:DD HH:mm:ss(0000:00:01 00:00:00)

diff gives you a duration as milliseconds if you don't specify the unit. 如果您不指定单位, diff会给您一个持续时间,以毫秒为单位。 And unfortunately, there is no way for format a duration with momentjs. 不幸的是,没有办法用momentjs格式化持续时间。

Your best bet is to use this plugin: https://github.com/jsmreese/moment-duration-format/ 你最好的选择是使用这个插件: https//github.com/jsmreese/moment-duration-format/

Or you can do it manually. 或者你可以手动完成。 Create a moment.duration() object and create the string manually. 创建一个moment.duration()对象并手动创建该字符串。

var from = moment();
var to   = moment().milliseconds(ms);
var dif = moment.duration(to.diff(from));
var string = dif.years() + "-" + dif.months() + "-" + dif.days() ...

But then you have to worry about padding zeros and it becomes a pain. 但是你必须担心填充零,这会变得很痛苦。 So just use the plugin. 所以只需使用插件即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用moment.js(时区)时以(“ YYYY-MM-DD HH:mm”)格式返回日期和时间 - Return date and time in (“YYYY-MM-DD HH:mm”) format when using moment.js (timezone) 使用moment.js将秒转换为hh:mm:ss格式 - Convert seconds into hh:mm:ss format using moment.js 使用 Iso yyyy-MM-dd HH:mm:ss 的无效日期 moment.js - Invalid Date moment.js using Iso yyyy-MM-dd HH:mm:ss Moment.js如何将MM / dd / yyyy HH:mm:ss.fff转换为往返日期/时间模式? - Moment.js How to convert MM/dd/yyyy HH:mm:ss.fff to round-trip date/time pattern? 如何使用 moment 或 js 将 DD-MM-YYYY HH:mm 格式转换为纪元时间? - How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js? 如何在 moment.js 中将秒转换为 HH:mm:ss - How to convert seconds to HH:mm:ss in moment.js 当日期为MM / DD / YYYY HH:mm:ss时,moment.js从现在的日期返回意外 - moment.js returning an unexpected fromNow date when date is MM/DD/YYYY HH:mm:ss 使用moment.js将日期时间格式化为DD-MMM-YYY HH:mm:ss - Format datetime to DD-MMM-YYY HH:mm:ss using moment.js 如何使用moment.js仅获取日期(DD-MM-YYYY)格式而不是日期时间? - How to get only date (DD-MM-YYYY) format not time with date using moment.js? 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM