Tengo un objeto Date y me gustaría mostrarlo en el siguiente formato:¿Cómo mostrar un objeto Date en un formato específico usando JavaScript?
var myDate = getDate();
// this format: "13 Jan 2012 11:00am";
¿Cómo sería eso posible?
Gracias,
Tengo un objeto Date y me gustaría mostrarlo en el siguiente formato:¿Cómo mostrar un objeto Date en un formato específico usando JavaScript?
var myDate = getDate();
// this format: "13 Jan 2012 11:00am";
¿Cómo sería eso posible?
Gracias,
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");
Tener un vistazo a la Date()
objeto: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Hay un montón de métodos útiles ....
Creo que esto puede ser útil para usted:
http://www.mattkruse.com/javascript/date/
hay una función getDateFromFormat() que se puede ajustar un poco para resolver su problema.
Hay una gran biblioteca de JavaScript que maneja esto muy bien, y solo 5,5 kb minificado.
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.
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>
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 –
que sólo funciona para Mozilla embargo. –
@William err nope - funciona en todos los navegadores. Es solo que Mozilla proporciona la lista de funciones ... también puede obtenerla aquí -> http://javascript.about.com/library/bldate1.htm – ManseUK