如何將秒轉換爲javascript中的日期時間對象。JavaScript將秒轉換爲日期對象
例子:
1.3308313703571
1.6324722385401
這是由一系列的點,當他們發生。我理解了1.23323秒以上,但我無法更改從api中拉出的值。
如何將秒轉換爲javascript中的日期時間對象。JavaScript將秒轉換爲日期對象
例子:
1.3308313703571
1.6324722385401
這是由一系列的點,當他們發生。我理解了1.23323秒以上,但我無法更改從api中拉出的值。
你可以嘗試這樣的:
function toDateTime(secs) {
var t = new Date(1970, 0, 1); // Epoch
t.setSeconds(secs);
return t;
}
信息上epoch date。
您的實例值有小數..看起來像你正試圖1.something秒轉換成一個日期..
同時檢查正確秒,日期轉換這個例子here ..你可以查看他們的js的來源。
/**
DateTime
-------
Author: Jamal BOUIZEM
Date: 20/02/2015
Version: 1.0.0
*/
var DateTime = (function(){
return {
instance: function(spec){
var that = {
h: spec.h || 0,
m: spec.m || 0,
s: spec.s || 0
};
var d = new Date();
var str = "";
function __init__(h, m, s)
{
that.h = h;
that.m = m;
that.s = s;
d.setHours(that.h);
d.setMinutes(that.m);
d.setSeconds(that.s);
};
that.get = function(){
d.setHours(that.h);
d.setMinutes(that.m);
d.setSeconds(that.s);
return d;
};
that.set = function(h, m, s){
__init__(h, m, s);
};
that.convertSecs = function(){
return (that.h * 3600) + (that.m * 60) + that.s;
};
that.secsToDate = function(seconds){
var ts = seconds % 60;
var tm = (seconds/60) % 60;
var th = (seconds/3600) % 60;
return DateTime.instance({ h: parseInt(th), m: parseInt(tm), s: parseInt(ts) });
};
that.add = function(d){
return that.secsToDate(that.convertSecs() + d.convertSecs());
};
that.subtract = function(d){
return that.secsToDate(that.convertSecs() - d.convertSecs());
};
__init__(that.h, that.m, that.s);
return that;
}
}
})();
這個問題似乎已經得到了回答,但這可能對那些試圖做類似於ruby的Time.at()方法的人有幫助。
function formatDateTime(input){
var epoch = new Date(0);
epoch.setSeconds(parseInt(input));
var date = epoch.toISOString();
date = date.replace('T', ' ');
return date.split('.')[0].split(' ')[0] + ' ' + epoch.toLocaleTimeString().split(' ')[0];
};
這些浮點數表示什麼樣的日期時間? – deceze 2011-01-06 04:32:18
這些是從零開始的一系列秒。這只是他們發生時的一系列問題。 – jhanifen 2011-01-06 04:34:25