2012-02-22 74 views
2

如何將這個時間11:34轉換爲javascript時間戳。任何可用的JavaScript功能。如何將時間轉換爲javascript-timestamps使用javascript或jquery?

我想爲我使用Flot庫創建一個浮點圖。在我的圖表時間x軸和y軸計數。爲了創建數據部分,我需要將時間轉換爲類似於API文檔中指定的時間戳。

http://people.iola.dk/olau/flot/API.txt

這是我的代碼

var datasets = { 
    "usa": { 
     label: "Logged Users", 
     data: [[10:55, 4], [11:00, 1], [11:05, 4], [11:10, 2], [11:15, 3], [11:20, 1], [11:25, 5]] 
    } 
}; 

if (datasets.length > 0){ 
    $.plot($("#placeholder"), datasets, { 
     yaxis: { min: 0,tickDecimals: 0 }, 
     xaxis: { mode: "time",timeformat: "%H:%M" } 
    }); 
} 

,因爲我指定的確切時間,而不是數它不會工作。所以我需要將其轉換爲時間戳格式。
請幫幫我。

感謝

+0

是記錄事件的時間戳還是什麼,更詳細地描述您想要的內容? – uday 2012-02-22 07:40:43

回答

4

使用Date object的一個實例:

var sTime = '11:34'; 
var oDate = new Date(); 
oDate.setUTCHours(
    parseInt(sTime.substr(0, 2), 10), 
    parseInt(sTime.substr(3, 2), 10), 
    0, 
    0 
); 
var sTimestamp = oDate.getTime(); 

另見this example

=== UPDATE ===

當時間是本地時間而不是UTC,那麼你可以設置時間:

oDate.setHours(
    parseInt(sTime.substr(0, 2), 10), 
    parseInt(sTime.substr(3, 2), 10), 
    0, 
    0 
); 

另見this example

P.s .: getTime()的結果是以毫秒爲單位。

=== UPDATE ===

要映射您當前的數據,你可以使用下面的腳本(與UTC的;如果你想當地時間消除在二傳手的UTC):

var aCountries = [ "usa" ]; 
var oDate = new Date(); 
oDate.setSeconds(0, 0); 
for (var i = 0; i < aCountries.length; i++) { 
    datasets[aCountries[i]].data = 
     datasets[aCountries[i]].data.map(function(oElement) { 
      oDate.setUTCHours(
       parseInt(oElement[0].substr(0, 2), 10), 
       parseInt(oElement[0].substr(3, 2), 10) 
      ); 
      return [ 
       oDate.getTime() 
       , oElement[1] 
      ]; 
     }); 
} 

而且請參閱this example

+0

請注意,時間戳總是包含「日期」部分(即當前日期)。除非您創建自己的類型,否則無法在JavaScript中創建「僅限小時」時間戳。 – 2012-02-22 08:14:48

0
var now = new Date(); 

    now.format("m/dd/yy"); 

// Returns, e.g., 6/09/07 

// Can also be used as a standalone function 
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); 
// Saturday, June 9th, 2007, 5:46:21 PM 

// You can use one of several named masks 
now.format("isoDateTime"); 
// 2007-06-09T17:46:21 

// ...Or add your own 
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"'; 
now.format("hammerTime"); 
// 17:46! Can't touch this! 

// When using the standalone dateFormat function, 
// you can also provide the date as a string 
dateFormat("Jun 9 2007", "fullDate"); 
// Saturday, June 9, 2007 

// Note that if you don't include the mask argument, 
// dateFormat.masks.default is used 
now.format(); 
// Sat Jun 09 2007 17:46:21 

// And if you don't include the date argument, 
// the current date and time is used 
dateFormat(); 
// Sat Jun 09 2007 17:46:22 

// You can also skip the date argument (as long as your mask doesn't 
// contain any numbers), in which case the current date/time is used 
dateFormat("longTime"); 
// 5:46:22 PM EST 

// And finally, you can convert local time to UTC time. Either pass in 
// true as an additional argument (no argument skipping allowed in this case): 
dateFormat(now, "longTime", true); 
now.format("longTime", true); 
// Both lines return, e.g., 10:46:21 PM UTC 

// ...Or add the prefix "UTC:" to your mask. 
now.format("UTC:h:MM:ss TT Z"); 
// 10:46:21 PM UTC 

Use all date time example .,so use it. 
-1

我發現jQuery Globalization Plugin日期解析工作效果最好。其他方法有跨瀏覽器問題和像date.js的東西在一段時間內沒有更新。

你也不需要頁面上的一個datePicker。您可以撥打與文檔中給出的示例相似的內容:

$.parseDate('yy-mm-dd', '2007-01-26'); 
相關問題