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
function msToTime(ms) {
let seconds = (ms / 1000).toFixed(1);
let minutes = (ms / (1000 * 60)).toFixed(1);
let hours = (ms / (1000 * 60 * 60)).toFixed(1);
let days = (ms / (1000 * 60 * 60 * 24)).toFixed(1);
if (seconds < 60) return seconds + " Sec";
else if (minutes < 60) return minutes + " Min";
else if (hours < 24) return hours + " Hrs";
else return days + " Days"
}
console.log(msToTime(new Date() - 1653991056493))
Related Topics
How to Create Expand/Collapse Function for Each Row of Table - Angular6
How to Change Color of Ag-Grid Cells for Dynamically Changing Data
Check If Current Time Is Between Two Given Times in JavaScript
It Is Possible to Call Class Function from Outside Class
Jquery: Simplest Way to Check Url for a Parameter and Pass It to Anchor Href If It Exists
Vuejs Reload If in the Same Route
Change Button Color When Click on the Button
Prevent React-Router History.Push from Reloading Current Route
Display Images in React Using Jsx Without Import
Check If a String Has White Space
What's the Best Way to Reload/Refresh an Iframe
Dynamically Change Background Color on Scroll
Why React Props Are Passed Undefined to the Child Component
Show Virtual Keyboard on Mobile Phones in JavaScript
Check If Image Exists on Server Using JavaScript
How to Download File in React Js
Disable/Hide Download Button in Iframe on Default Pdf Viewer
How to Reset the Bootstrap Modal When It Gets Closed and Open It Fresh Again