2011-01-06 10 views
11

Come convertire i secondi in un oggetto datetime in javascript.Javascript converti i secondi in un oggetto data

Esempi:

1,3308313703571

1,6324722385401

Questo è da una serie di punti e quando si verificano. Capisco 1,23323 in più di secondi, ma non posso cambiare il valore, essendo estratto da un'api.

+0

Che tipo di datetime rappresentano questi numeri in virgola mobile? – deceze

+0

Questi sono una serie di secondi, a partire da zero. È solo una serie di punti rappresentati da quando si sono verificati. – jhanifen

+1

Penso che il problema che stiamo avendo qui è che i tuoi esempi che vuoi convertire non hanno senso. Si suppone che sia il numero di secondi da un determinato momento (1,3 secondi dopo il 1 gennaio 1970?) –

risposta

32

Si può provare in questo modo:

function toDateTime(secs) { 
    var t = new Date(1970, 0, 1); // Epoch 
    t.setSeconds(secs); 
    return t; 
} 

Info sulla epoch date.

1

i tuoi valori di esempio hanno un decimale .. guardare come si sta tentando di convertire secondi 1.something in una data ..

controllare Nel frattempo questo esempio here sui secondi corrette per la conversione data .. si poteva vedere il loro fonti js.

+1

Grazie, funziona. Ecco il codice: 'var d = new Date(); d.setTime (secs * 1000); ' – ariscris

-3
/** 
    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; 
     } 
    } 
})(); 
0

La questione sembra essere già stata data risposta, ma questo può essere utile per coloro che tentano di fare qualcosa di simile per il metodo() di 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]; 
};