Fondamentalmente, ricevo i timestamp grezzi e ho bisogno di formattarli in formato HH: MM: SS.Come si formatta un timestamp in Javascript per visualizzarlo nei grafici? UTC sta bene
risposta
andrò con il presupposto che si intende timestamp Unix:
var formatTime = function(unixTimestamp) {
var dt = new Date(unixTimestamp * 1000);
var hours = dt.getHours();
var minutes = dt.getMinutes();
var seconds = dt.getSeconds();
// the above dt.get...() functions return a single digit
// so I prepend the zero here when needed
if (hours < 10)
hours = '0' + hours;
if (minutes < 10)
minutes = '0' + minutes;
if (seconds < 10)
seconds = '0' + seconds;
return hours + ":" + minutes + ":" + seconds;
}
var formattedTime = formatTime(1266272460);
document.write(formattedTime);
Grazie per la risposta, in realtà intendevo i timestamp di Javascript. javascript timestamp = unix timestamps * 1000 perché il timestamp javascript è il numero di ms dal 1 gennaio 1970 00:00:00 UTC – rmk
L'ho modificato per i miei scopi, che includevano l'aggiunta di un anno, mese e giorno. Due sorprese che ho riscontrato sono che vuoi dt.getDate() invece di dt.getDay() (getDay restituisce il giorno della settimana); e che dt.getYear() restituisce ciò che normalmente pensiamo come l'anno (ad esempio, 2011) in IE 6, ma restituisce l'anno 1900 negli altri browser che ho testato (Chrome, Opera, Firefox). Solo qualcosa di cui essere a conoscenza. –
Ciò visualizzare l'ora corrente nel formato richiesto (HH:MM:SS
)
function dostuff()
{
var item = new Date();
alert(item.toTimeString());
}
Breve e dolce. Grazie! –
Sto ricevendo qualcosa di più lungo: 20:07:30 GMT + 0100 (ora standard dell'Europa centrale) – Jeffz
Ecco una funzione che fornisce una formattazione flessibile di una data in UTC. Si accetta una stringa di formato simile a quello di SimpleDateFormat di Java:
function formatDate(date, fmt) {
function pad(value) {
return (value.toString().length < 2) ? '0' + value : value;
}
return fmt.replace(/%([a-zA-Z])/g, function (_, fmtCode) {
switch (fmtCode) {
case 'Y':
return date.getUTCFullYear();
case 'M':
return pad(date.getUTCMonth() + 1);
case 'd':
return pad(date.getUTCDate());
case 'H':
return pad(date.getUTCHours());
case 'm':
return pad(date.getUTCMinutes());
case 's':
return pad(date.getUTCSeconds());
default:
throw new Error('Unsupported format code: ' + fmtCode);
}
});
}
si potrebbe usare in questo modo:
formatDate(new Date(timestamp), '%H:%m:%s');
- 1. Creazione timestamp UTC in Javascript
- 2. Come si formatta il datetime nei binari?
- 3. Converti OffsetDateTime in UTC Timestamp
- 4. Come si formatta una data Javascript?
- 5. momentoJS ottenere il timestamp UTC
- 6. SensorEvent.timestamp a (UTC) timestamp assoluto?
- 7. come generare un unix timestamp UTC in C#
- 8. Come si formatta un decimale?
- 9. PHP time() restituisce un timestamp GMT/UTC?
- 10. MySql - Colonna TimeStamp SELECT in formato UTC
- 11. Javascript per convertire UTC in ora locale
- 12. Con NodaTime, come si formatta un ZonedDateTime nella cultura corrente
- 13. Postgres: Come si formatta un timestamp int come stringa di data leggibile?
- 14. Come si ottiene un timestamp in ClojureScript?
- 15. Time.time() di Python restituisce un timestamp in UTC?
- 16. Come ottenere il timestamp Unix in MySQL dall'ora UTC?
- 17. Inserzioni nei grafici in R
- 18. Esiste una funzione mysql che formatta un timestamp unix?
- 19. Caratteri nei grafici R
- 20. Come si formatta XAML in Expression Blend?
- 21. creare javascript UTC data
- 22. Come convertire un timestamp UTC in data e ora del sistema in ABAP
- 23. Come si formatta come indicato di seguito?
- 24. javascript get timestamp nel fuso orario del sistema non in UTC
- 25. MySql TimeStamp e JavaScript Time
- 26. Flusso massimo nei grafici dinamici
- 27. Come si formatta un BigDecimal in modo che mostri tutte le cifre decimali necessarie?
- 28. Timestamp nei registri di upstart
- 29. Come si formatta una data con Dart?
- 30. come applicare la programmazione in parallelismo nei problemi grafici?
Quando si dice "timestamp prime", cosa si intende il numero di tick (vale a dire un numero intero grande che inizia con "1")? –
Intendo un timestamp javascript. – rmk