Created
January 29, 2014 23:42
-
-
Save jomasero/f3957bab0327db056f13 to your computer and use it in GitHub Desktop.
Funciones para el formato de fechas en JS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ///*** TDCVUtilFechas: Namespace con funciones de formato de fechas y tiempo. ***/// | |
| var TDCVUtilFechas = {}; | |
| /** Devuelve un string con el formato preferido para fechas en español. */ | |
| TDCVUtilFechas.prettyFormat = function(dateString) { | |
| var DIAS_SEMANA = ["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado"]; | |
| var MESES = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]; | |
| var dateObject = (dateString === "") ? new Date() : new Date(dateString); | |
| var hora = dateObject.getHours(); | |
| var sufHora = (hora < 12) ? "a.m." : "p.m."; | |
| if (hora > 12) { hora -= 12; } | |
| return DIAS_SEMANA[dateObject.getDay()] + " " + dateObject.getDate() | |
| + " de " + MESES[dateObject.getMonth()] + ", " + dateObject.getFullYear() | |
| + "; " + hora + ":" + ("0" + dateObject.getMinutes()).slice(-2) + " " + sufHora; | |
| }; | |
| /** Devuelve un string con el formato adecuado para el valor de un elemento <input type="date"> */ | |
| TDCVUtilFechas.formatearFechaInput = function(dateString) { | |
| var dateObject = (dateString === "") ? new Date() : new Date(dateString); | |
| return dateObject.getFullYear() + "-" + ("0" + (dateObject.getMonth() + 1)).slice(-2) + "-" | |
| + ("0" + dateObject.getDate()).slice(-2); | |
| }; | |
| /** Devuelve un string con el formato adecuado para el valor de un elemento <input type="time"> */ | |
| TDCVUtilFechas.formatearHoraInput = function(dateString) { | |
| var dateObject = (dateString === "") ? new Date() : new Date(dateString); | |
| return ("0" + dateObject.getHours()).slice(-2) + ":" + ("0" + dateObject.getMinutes()).slice(-2) + ":" | |
| + ("0" + dateObject.getSeconds()).slice(-2); | |
| }; | |
| /** Combina los valores de un input de tipo date y un input de tipo time en un objeto Date. */ | |
| TDCVUtilFechas.inputStringToDate = function(inputDateVal, inputTimeVal) { | |
| var dateObject = new Date(inputDateVal + "T" + inputTimeVal); | |
| dateObject.setMinutes(dateObject.getMinutes() + dateObject.getTimezoneOffset()); | |
| return dateObject; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment