Eventuali duplicati:
Formatting a date in JavaScriptJavascript Date.toString() formattazione?
Ho il seguente pezzo di script. È un dispositivo di scorrimento HTML5 con un intervallo di date. Il cursore utilizza un timestamp unix e voglio visualizzare la selezione corrente in un formato leggibile.
Questo sta funzionando bene ma sta uscendo come "Mer 16 Maggio 2012 08:07:30 GMT + 0100 (GMT Daylight Time)" nonostante io specifichi il formato come "aaaa-MM-gg HH: mm: ss".
Qualche idea sul perché non viene emessa nel mio formato?
<input id="slider3" type="range" min="1337149800" max="1337160600" step="450" onchange="printValue('slider3','rangeValue3')"/>
<input id="rangeValue3" type="text" size="90"/>
<script>
function printValue(sliderID, textbox) {
var x = document.getElementById(textbox);
var y = document.getElementById(sliderID);
var d1=new Date(y.value*1000);
var newtimestamp = d1.toString("yyyy-MM-dd HH:mm:ss");
x.value = newtimestamp;
}
</script>
EDIT: Grazie per la spinta nella giusta direzione, infatti, che non era possibile con Data. Questo funziona invece: Date
oggetto
<input id="slider3" type="range" min="1337149800" max="1337160600" step="450" onchange="printValue('slider3','rangeValue3')"/>
<input id="rangeValue3" type="text" size="90"/>
<script>
function printValue(sliderID, textbox) {
var x = document.getElementById(textbox);
var y = document.getElementById(sliderID);
var d1=new Date(y.value*1000);
var curr_year = d1.getFullYear();
var curr_month = d1.getMonth() + 1; //Months are zero based
if (curr_month < 10)
curr_month = "0" + curr_month;
var curr_date = d1.getDate();
if (curr_date < 10)
curr_date = "0" + curr_date;
var curr_hour = d1.getHours();
if (curr_hour < 10)
curr_hour = "0" + curr_hour;
var curr_min = d1.getMinutes();
if (curr_min < 10)
curr_min = "0" + curr_min;
var curr_sec = d1.getSeconds();
if (curr_sec < 10)
curr_sec = "0" + curr_sec;
var newtimestamp = curr_year + "-" + curr_month + "-" + curr_date + " " + curr_hour + ":" + curr_min + ":" + curr_sec;
x.value = newtimestamp;
}
http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript –
Grazie Mat - Sono stato in grado di mettere insieme una soluzione dalle risposte al riguardo. Ho modificato la domanda con una soluzione. – user1107685
Si prega di mettere la risposta in una risposta al posto della domanda. – Alex