I have the time as milliseconds, but I want the time after conversion like 00:00:00
.
Ex: In milliseconds=86400000. I want how many hours in that milliseconds like, 00:00:00
How to get it in JavaScript?
I have the time as milliseconds, but I want the time after conversion like 00:00:00
.
Ex: In milliseconds=86400000. I want how many hours in that milliseconds like, 00:00:00
How to get it in JavaScript?
How about doing this by creating a function in javascript as shown below:
function msToTime(duration) { var milliseconds = parseInt((duration%1000)/100) , seconds = parseInt((duration/1000)%60) , minutes = parseInt((duration/(1000*60))%60) , hours = parseInt((duration/(1000*60*60))%24); hours = (hours
To Convert time in millisecond to human readable format.
function timeConversion(millisec) { var seconds = (millisec / 1000).toFixed(1); var minutes = (millisec / (1000 * 60)).toFixed(1); var hours = (millisec / (1000 * 60 * 60)).toFixed(1); var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1); if (seconds
I had the same problem, this is what I ended up doing:
function parseMillisecondsIntoReadableTime(milliseconds){ //Get hours from milliseconds var hours = milliseconds / (1000*60*60); var absoluteHours = Math.floor(hours); var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours; //Get remainder from hours and convert to minutes var minutes = (hours - absoluteHours) * 60; var absoluteMinutes = Math.floor(minutes); var m = absoluteMinutes > 9 ? absoluteMinutes : '0' + absoluteMinutes; //Get remainder from minutes and convert to seconds var seconds = (minutes - absoluteMinutes) * 60; var absoluteSeconds = Math.floor(seconds); var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds; return h + ':' + m + ':' + s; } var time = parseMillisecondsIntoReadableTime(86400000); alert(time);
This one returns time like youtube videos
function getYoutubeLikeToDisplay(millisec) { var seconds = (millisec / 1000).toFixed(0); var minutes = Math.floor(seconds / 60); var hours = ""; if (minutes > 59) { hours = Math.floor(minutes / 60); hours = (hours >= 10) ? hours : "0" + hours; minutes = minutes - (hours * 60); minutes = (minutes >= 10) ? minutes : "0" + minutes; } seconds = Math.floor(seconds % 60); seconds = (seconds >= 10) ? seconds : "0" + seconds; if (hours != "") { return hours + ":" + minutes + ":" + seconds; } return minutes + ":" + seconds; }
Output:
my solution
var sunriseMills = 1517573074000; // sunrise in NewYork on Feb 3, 2018 - UTC time var offsetCityMills = -5 * 3600 * 1000; // NewYork delay to UTC var offsetDeviceMills = new Date().getTimezoneOffset() * 60 * 1000 ; // eg. I live in Romania (UTC+2) >> getTimezoneOffset() = 120 var textTime = new Date(sunriseMills + offsetCityMills + offsetDeviceMills) .toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' });
textTime will become '7.04 AM'
Based on @Chand answer. This is the implementation in Typescript. A bit safer than coercing types in JS. If you remove the type annotation should be valid JS. Also using new string functions to normalise the time.
function displayTime(millisec: number) { const normalizeTime = (time: string): string => (time.length === 1) ? time.padStart(2, '0') : time; let seconds: string = (millisec / 1000).toFixed(0); let minutes: string = Math.floor(parseInt(seconds) / 60).toString(); let hours: string = ''; if (parseInt(minutes) > 59) { hours = normalizeTime(Math.floor(parseInt(minutes) / 60).toString()); minutes = normalizeTime((parseInt(minutes) - (parseInt(hours) * 60)).toString()); } seconds = normalizeTime(Math.floor(parseInt(seconds) % 60).toString()); if (hours !== '') { return `${hours}:${minutes}:${seconds}`; } return `${minutes}:${seconds}`; }