2012-03-02 16 views

Respuesta

5

hay muchos paquetes de formato fecha disponibles para Javascript, he tenido un gran éxito con Steven Levithan's dateformat.

dateFormat(getDate(), "dd mmm yyyy hh:MMtt"); 

Editar: También agrega un método format a Date.prototype, si te gusta ese estilo:

getDate().format("dd mmm yyyy hh:MMtt"); 
8

Hay una gran biblioteca de JavaScript que maneja esto muy bien, y solo 5,5 kb minificado.

http://momentjs.com/

se ve algo como esto:

moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am 
moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM" 

También se puede pasar en fechas como un String con un formato, o un objeto Date.

var date = new Date(); 
moment(date); // same as calling moment() with no args 

// Passing in a string date 
moment("12-25-1995", "MM-DD-YYYY"); 

también tiene un gran soporte para idiomas distintos de inglés, al igual que, ruso, japonés, árabe, español, etc ..

Mira la docs.

6

Si no desea utilizar cualquier biblioteca:

<script type="text/javascript"> 

    var myDate = new Date(); 

    var month=new Array(); 
    month[0]="Jan"; 
    month[1]="Feb"; 
    month[2]="Mar"; 
    month[3]="Apr"; 
    month[4]="May"; 
    month[5]="Jun"; 
    month[6]="Jul"; 
    month[7]="Aug"; 
    month[8]="Sep"; 
    month[9]="Oct"; 
    month[10]="Nov"; 
    month[11]="Dec"; 
    var hours = myDate.getHours(); 
    var minutes = myDate.getMinutes(); 
    var ampm = hours >= 12 ? 'pm' : 'am'; 
    hours = hours % 12; 
    hours = hours ? hours : 12; 
    minutes = minutes < 10 ? '0'+minutes : minutes; 
    var strTime = hours + ':' + minutes + ampm; 
    // e.g. "13 Nov 2016 11:00pm"; 
    alert(myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime); 
</script> 
+2

myDate.getDay() le ofrece el día de la semana. Use getDate() en su lugar. Fuente: http://www.w3schools.com/jsref/jsref_obj_date.asp –

Cuestiones relacionadas