Prefacio
El formato de fecha de JavaScript es bastante común en el desarrollo diario. Entonces, a continuación compartiré con ustedes dos ejemplos de uso de la función de formato de formato de tiempo JavaScript. Echemos un vistazo.
Método 1
Date.prototype.pattern = function (fmt) {var o = {"m+": this.getMonth ()+1, // mes "d+": this.getDate (), // date "h+": this.gethours ()%12 == 0? 12: this.gethours ()%12, // hora "H+": this.gethours (), // hora "m+": this.getminutes (), // min "s+": this.getSeconds (), // segundo "q+": math.floor ((this.getmonth ()+3)/3) var Week = {"0": "/u65e5", "1": "/u4e00", "2": "/u4e8c", "3": "/u4e09", "4": "/u56db", "5": "/u4e94", "6": "/u516d"}; if (/(y+)/. test (fmt)) {fmt = fmt.replace (regexp. $ 1, (this.getblyar ()+""). Substr (4 - Regexp. $ 1.length)); } if (/(e+)/. test (fmt)) {fmt = fmt.replace (regexp. $ 1, ((regexp. $ 1. 1.length> 1)? (Regexp. $ 1. 1.length> 2? "/u661f/u671f": "/u5468"): ")+Semana [this.getay ()") "]); } for (var k en o) {if (new Regexp ("("+ k+ ")"). test (fmt)) {fmt = fmt.replace (regexp. $ 1, (regexp. $ 1.length == 1)? (o [k]): ("00"+ o [k]). }} return fmt; }Métodos de llamadas
var fecha = nueva fecha (); Window.alert (date.pattern ("yyyyy-mm-dd hh: mm: ss"));Método 2:
// expansión de la fecha, convertir la fecha en cadena en el formato especificado // mes (m), día (d), horas (h), minutos (m), segundos (s) y cuartos (q) se pueden usar para 1-2 poseedores de 1-2, // año (y) se pueden usar para 1-4 titulares de lugar y medios de posición y milisegundos se pueden usar para 1 hold Date ()). Format ("yyyy-mm-dd hh: mm: ss.s") ==> 2006-07-02 08: 09: 04.423 // (new ()). Format ("yyyy-md h: m: ss") ==> 2006-7-2 8: 9: 4.18 date.pototype.format = function (fmt) "M+": this.getMonth ()+1, // mes "d+": this.getDate (), // day "H+": this.gethours (), // Hours "M+": this.getminutes (), // min "S+": this.getSeconds (), // segundos "q+": math.floor ((this.getmonth () /3), /3), /3), /3), "S": this.getMilliseConds () // MilliseConds}; if (/(y+)/.test(fmt)) fmt = fmt.replace (regexp. $ 1, (this.getblyar () + "") .substr (4 - regexp. $ 1.length)); para (var k en o) if (nuevo regexp ("(" + k + ")"). test (fmt)) fmt = fmt.replace (regexp. $ 1, (regexp. $ 1.length == 1)? (o [k]): (("00" + o [k]). sustr (((" + o [k]). longitud))); return fmt;}Método de llamadas:
var harooms1 = new Date (). Format ("aaa yyy-mm-dd"); var harooms2 = new Date (). Format ("aaa yyy-mm-dd hh: mm: ss"); alerta (haraoms1); alerta (haraoms2);Resumir
Lo anterior se trata de este artículo. Los dos métodos anteriores son de Internet y están disponibles después de la verificación y las pruebas. También son más convenientes. Cuando los usas, puedes encapsularlos. Simplemente llámelos directamente en su función. ¡Estas dos funciones de formato de tiempo siguen siendo relativamente buenas! Espero que te ayude en tu estudio o trabajo. Si tiene alguna pregunta, deje un mensaje para comunicarse.