我有時間毫秒,但我想要轉換後的時間,如00:00:00
。例如:如何在JavaScript中將時間毫秒轉換爲小時,分鐘,秒格式?
例如:以毫秒爲單位= 86400000。我想要幾毫秒的時間,如00:00:00
如何使用JavaScript?
我有時間毫秒,但我想要轉換後的時間,如00:00:00
。例如:如何在JavaScript中將時間毫秒轉換爲小時,分鐘,秒格式?
例如:以毫秒爲單位= 86400000。我想要幾毫秒的時間,如00:00:00
如何使用JavaScript?
怎麼樣,如下圖所示創建一個JavaScript函數這樣做:
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 < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
我用這種方法將毫秒數轉換爲小時數:分鐘數:秒格式。但是我沒有正確理解。它只給出21:11:05所有毫秒。我可以執行此操作嗎? – cheliyan
var s = st_date +''+ start; \t \t var e = end_date +''+ end; \t \t var bits = s.split(/ \ D /); \t \t var bits1 = e.split(/ \ D /); var date = new Date(bits [0],--bits [1],bits [2],bits [3],bits [4],bits [5]); var date1 = new Date(bits1 [0],--bits1 [1],bits1 [2],bits1 [3],bits1 [4],bits1 [5]); – cheliyan
var t1 =日期。getTime() \t \t var t2 = date1.getTime() \t \t var t3 = date1.getTime() - date.getTime(); \t \t \t \t \t \t \t 變種\t秒; \t \t var minutes; \t \t變種毫秒= parseInt函數((T3%1000)/ 100) \t \t \t,秒= parseInt函數((T3/1000)%60) ,分= parseInt函數((T3 /(1000 * 60))%60 ) ,hours = parseInt((t3 /(1000 * 60 * 60))%24); 小時=(小時<10)? 「0」+小時:小時; 分鐘=(分鐘<10)? 「0」+分鐘:分鐘; 秒=(秒<10)? 「0」+秒:秒; \t \t duration = hours +「:」+ minutes +「:」+ seconds; \t \t alert('dur ::'+ t3); \t \t alert('duration:'+ duration); – cheliyan
要在毫秒轉換時間爲人類可讀的格式。
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 < 60) {
return seconds + " Sec";
} else if (minutes < 60) {
return minutes + " Min";
} else if (hours < 24) {
return hours + " Hrs";
} else {
return days + " Days"
}
}
我發現這個最有用,最好的答案似乎沒有在我的測試中處理好幾個小時,這也給了你幾天的選擇。 – ak85
我有同樣的問題,這是我落得這樣做:
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);
這一個返回時間,如YouTube視頻
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;
}
輸出:
我的解決方案
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將成爲'7.04 AM'
基於@Chand的回答。這是在Typescript中的實現。比在JS中強制類型更安全一些。如果刪除類型註釋應該是有效的JS。還使用新的字符串函數來標準化時間。 [在JavaScript中格式化日期]的
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}`;
}
完全相同的副本(http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript) –
沒有事實並非如此。格式化JavaScript中的日期是指日期,這是指時間。例如:1天與1970年1月1日不一樣 –