Convert Milliseconds to Hours and Minutes Using Momentjs

How do I convert milliseconds to dd:hh:mm using moment.js?

Sounds like humanizeduration is what you are looking for:
humanizeDuration(97320000) // '1 day, 3 hours, 2 minutes'

Here is the github link:
https://github.com/EvanHahn/HumanizeDuration.js

Converting Milliseconds to Minutes and Seconds in momentjs

My solution is:

const millisecondsToMinutesSeconds = (ms) => {
let duration = moment.duration(ms, 'milliseconds');
let fromMinutes = Math.floor(duration.asMinutes());
let fromSeconds = Math.floor(duration.asSeconds() - fromMinutes * 60);

return Math.floor(duration.asSeconds()) >= 60 ? (fromMinutes<= 9 ? '0'+fromMinutes : fromMinutes) +':'+ (fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds)
: '00:'+(fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds);

};

Moment js convert milliseconds into date and time

Moment parser

moment(1454521239279).format("DD MMM YYYY hh:mm a") //parse integer
moment("1454521239279", "x").format("DD MMM YYYY hh:mm a") //parse string

Moment unix method

moment.unix(1454521239279/1000).format("DD MMM YYYY hh:mm a")

MomentJs convert milliseconds to days

You need to use asDays() if you want 377.7613437963 as output.

As the docs says:

As with the other getters for durations, moment.duration().days() gets the days (0 - 30).

moment.duration().asDays() gets the length of the duration in days.

Here a live example:

var duration = moment.duration(32638528433, 'milliseconds');var days = duration.asDays();console.log(days);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

How to use format() on a moment.js duration?

We are looking into adding some kind of formatting to durations in moment.js. See https://github.com/timrwood/moment/issues/463

A couple other libraries that might help out are http://countdownjs.org/ and https://github.com/icambron/twix.js

Convert milliseconds to hours or minute or hour or day in javascript

In fact here your code seems to work fine.

Reading the Date documentation :

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

So when you're doing new Date(1653991056493) it's 1653991056493ms after Jan 1st 1970 which is 19143.4 days.


If you want the ms between a date and the current date, you can just substract the current date with the timestamp

new Date() - 1653991056493