Prefacio
En el desarrollo de JavaScript front-end, a menudo encontramos el problema de la fecha y hora de procesamiento. A menudo usamos muchas funciones de procesamiento para completar un efecto simple de visualización de fecha y hora. Para mejorar la velocidad de desarrollo del sitio web, puede encapsular el código principal en una función separada y llamarlo directamente cuando sea necesario. Echemos un vistazo a las clases de procesamiento de tiempo que ha resumido. Simplemente llámelo directamente cuando lo necesite.
var myDate = new Date (); mydate.getyear (); // Obtenga el año actual (2 dígitos) mydate.getblyear (); // Obtenga el año completo (4 dígitos, 1970-????) mydate.getmonth (); // Obtenga el mes actual (0-11, 0 representa enero) myDate.getDate (); // Obtenga el día actual (1-31) mydate.getday (); // Obtenga la semana actual X (0-6, 0 representa el domingo) mydate.gettime (); // Obtenga la hora actual (milisegundos a partir de 1970.1.1) mydate.gethours (); // Obtenga el número actual de horas (0-23) mydate.getminutes (); // Obtenga el número actual de minutos (0-59) mydate.getSeconds (); // Obtenga el número actual de segundos (0-59) mydate.getMilliseConds (); // Obtenga el número actual de milisegundos (0-999) mydate.tolocaledateString (); // Obtenga la fecha actual var myTime = myDate.TOLOCALETIMETRING (); // Obtenga la hora actual mydate.tolocaleString (); // Obtenga la hora actual mydate.tolocaleString (); // Obtenga la hora actual mydate.tolocaleString (); // Obtenga la fecha de la lista de la lista de métodos de la biblioteca de scripts de script de fecha y hora. Fecha.prototype.maxdayofdate obtenga el número máximo de días del mes en que la fecha es fecha.prototype.weeknuMoFyear determina la semana del año en que la fecha es la fecha de cadena de cadena hasta la fecha tipo ISVALIDDATE VERIFIT code://--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Judge leap year // ----------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------- SS/SS/S/S segundos // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------- 100)); str = str.replace (/mm/,this.getmonth ()> 9? this.getMonth (). toString (): '0' + this.getMonth ()); str = str.replace (/m/g,this.getmonth ()); str = str.replace (/w | w/g, semana [this.getday ()]); str = str.replace (/dd|dd/,this.getDate ()> 9? this.getDate (). toString (): '0' + this.getDate ()); str = str.replace (/d|d/g,this.getdate ()); str = str.replace (/hhhn|hh/,this.gethours ()> 9? this.gethours (). toString (): '0' + this.gethours ()); str = str.replace (/h|h/g,this.gethours ()); str = str.replace (/mm/,this.getminutes ()> 9? this.getminutes (). toString (): '0' + this.getminutes ()); str = str.replace (/m/g,this.getminutes ()); str = str.replace (/ss|ss/,this.getSeconds ()> 9? this.getSeconds (). toString (): '0' + this.getSeconds ()); str = str.replace (/s|s/g,this.getSeconds ()); regresar str; } //+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- DateOne.substring (5, dateOne.lastIndexof ('-')); var oneday = dateOne.substring (dateOne.length, dateOne.lastIndexOf ('-')+1); var oneyear = dateOne.substring (0, dateOne.IndexOf ('-')); var twomonth = dataTwo.substring (5, Datetwo.lastIndexof ('-')); var twoday = dataTwo.substring (datetwo.length, datetwo.lastindexof ('-')+1); var thoyear = dataTwo.substring (0, Datetwo.indexof ('-')); var cha = ((date.Parse (onemonth+'/'+oneday+'/'+oneYear)- date.Parse (twomonth+'/'+twoday+'/'+twoyear))/86400000); devolver math.abs (cha); } //+----------------------------------------------------------------------------------------------------------------------------------------------- switch (strinterval) {case 's': return new Date (date.Parse (dttmp) + (1000 * número)); caso 'n': devolver nueva fecha (date.Parse (dttmp) + (60000 * número)); caso 'H': devolver nueva fecha (date.Parse (dttmp) + (3600000 * número)); caso 'd': devolver nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + (86400000 * número)); Caso 'W': devolver una nueva fecha (date.Parse (dttmp) + ((86400000 * 7) * número)); caso 'Q': return New Date (dttmp.getblyear (), (dttmp.getmonth ()) + número*3, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds ();); caso 'M': return New Date (dttmp.getblyear (), (dttmp.getmonth ()) + número, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds ()); caso 'Y': return new Date ((dttmp.getblyear () + número), dttmp.getmonth (), dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds ()); }} //+--------------------------------------------------------------------------------------------------------------------------------------- //+--------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------- 60000); caso 'H': return parseInt ((dtend - dtStart) / 3600000); caso 'd': return parseint ((dtend - dtStart) / 86400000); caso 'W': return parseInt ((dtend - dtStart) / (86400000 * 7)); Case 'M': return (dtend.getMonth ()+1)+((dtend.getblyaryar () - dtStart.getTlyLeear ()*12) - (dtStart.getMonth ()+1); case 'y': return dtend.getblyear () - dtStart.getblyear (); }} //+----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- + = 'Semana' + Semana [myDate.getDay ()]; } return str; } //+------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- //If the format satisfies YYYY-(/)MM-(/)DD or YYYY-(/)M-(/)DD or YYYY-(/)M-(/)D or YYYY-(/)MM-(/)D, it will be replaced with '' //In the database, the legal date can be: YYYY-MM/DD(2003-3/21), and the database will Convertir automáticamente a yyyy-mm-dd format var s = sdate.replace (/[/d] {4,4} [/-/] {1} [/d] {1,2} [/-/] {1} [/d] {1,2}/g, ''); if (s == '') // El formato de instrucción satisface yyy-mm-dd o yyyy-m-dd o yyyy-md o yyyyy-mm-d {var t = new date (sdate.replace (//-/g, '/')); var ar = sdate.split (/[-/:]/); if (ar [0]! = t.getyear () || ar [1]! = t.getMonth ()+1 || ar [2]! = t.getDate ()) {// alert ('formato de fecha incorrecta! devolver falso; }} else {// alerta ('Formato de fecha incorrecta! El formato es: aaa yyy-mm-dd o aaaa yyyy/mm/dd. Tenga en cuenta el año bisiesto'); devolver falso; } return verdadero; } //+------------------------------------------------------------------------------------------------------------------------------------------- }) $/; var r = str.match (reg); if (r == null) return false; r [2] = R [2] -1; var d = nueva fecha (r [1], r [2], r [3], r [4], r [5], r [6]); if (d.gettylyar ()! = r [1]) return false; if (d.getMonth ()! = r [2]) return false; if (d.getDate ()! = r [3]) return false; if (d.gethours ()! = r [4]) return false; if (d.getMinutes ()! = R [5]) return false; if (d.getSeconds ()! = r [6]) return false; devolver verdadero; } //+------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- myArray [3] = myDate.GetHours (); myArray [4] = myDate.getMinutes (); myArray [5] = myDate.getSeconds (); devolver myArray; } //+------------------------------------------------------------------------------------------------------------------------------------------- = ['Día', 'uno', 'dos', 'tres', 'cuatro', 'cinco', 'Six']; switch (intervalo) {case 'y': partstr = mydate.getblyear (); break; caso 'm': partstr = myDate.getMonth ()+1; ruptura; caso 'd': partstr = myDate.getDate (); break; caso 'W': PartStr = Week [myDate.getday ()]; break; case 'ww': partstr = myDate.weeknumofyear (); break; caso 'H': PartStr = Week [myDate.getday ()]; break; case 'ww': partstr = myDate.weeknumofyear (); break; caso 'H': PartStr = myDate.Gethours (); break; case 'n': partstr = myDate.getMinutes (); break; case 's': partstr = myDate.getSeconds (); break; } return PartStr; } //+--------------------------------------------------------------------------------------------------------------------------------------- (nueva fecha (ary [0], ary [1] +1,1)); var date2 = date1.dateadd (1, 'm', 1); VAR result = Datediff (date1.Format ('yyyy-mm-dd'), date2.Format ('yyyy-mm-dd')); resultado de retorno; } //+----------------------------------------------------------------------------------------------------------------------------------------------- mydate.toarray (); var año = ary [0]; var mes = ary [1] +1; var día = ary [2]; document.write ('<script language = vBscript/n'); document.write ('myDate = datue (' '+mes+'-'+día+'-'+año+' ') /n'); document.write ('result = datePart (' ww ', mydate) /n'); document.write (' /n'); resultado de retorno; } //+----------------------------------------------------------------------------------------------------------------------------------------------- Formato mm/dd/aaa mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd //+--------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------- Fecha (ary [0],-ary [1], ary [2]); } return mydate; } Para mostrar: Fecha actual más hora (como: 2009-06-12 12:00) Función CurrentTime () {var Now = new Date (); var año = ahora.getTlyar (); // año var mes = ahora.getMonth () + 1; // mes var day = ahora.getDate (); // día var hh = ahora.gethours (); // Time var mm = ahora.getminutes (); // minuto var clock = año + "-"; if (mes <10) reloj += "0"; reloj + = mes + "-"; if (día <10) reloj += "0"; reloj + = día + ""; if (hh <10) reloj += "0"; reloj + = hh + ":"; if (mm <10) reloj += '0'; reloj += mm; regresar (reloj); }Resumir
Lo anterior es el contenido completo del resumen del procesamiento de tiempo de JavaScript. Espero que sea útil para todos en el futuro usando JavaScript.