2012-09-05 11 views
21

Duplicar posibles:
Formatting a date in JavaScriptFormato de Javascript Date.toString()?

tengo el siguiente fragmento de guión. Es un control deslizante HTML5 con un rango de fechas. El control deslizante usa una marca de tiempo de Unix y quiero mostrar la selección actual en un formato legible.

Esto funciona bien pero está saliendo como "mié 16 de mayo de 2012 08:07:30 GMT + 0100 (GMT horario de verano)" a pesar de que especifico el formato como "aaaa-MM-dd HH: mm: ss".

¿Alguna idea de por qué no se está produciendo en mi 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: Gracias por el empujón en la dirección correcta, en efecto, que no era posible con la fecha. Esto funciona en su lugar: Date objeto

<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; 
    } 

+0

http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript –

+0

Gracias Mat - Pude juntar una solución de las respuestas sobre eso. He editado la pregunta con una solución. – user1107685

+0

Por favor ponga su respuesta en una respuesta en lugar de la pregunta. – Alex

Respuesta

7

de JavaScript no soporta eso. Hay muchas bibliotecas para hacer esto por ti.

+0

Gracias - no me di cuenta de eso. He editado mi pregunta con una solución ahora. – user1107685

+8

@ user1107685 No añada respuestas a las preguntas. Solo agrega tu propia respuesta. – k0pernikus

+2

Si hay muchas bibliotecas, nombrar unas pocas no debería haber hecho daño ... –

Cuestiones relacionadas