Este artículo comparte con usted la fecha actual y la operación de formato de JS. El contenido específico es el siguiente
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 fecha y la hora
Lista de métodos de biblioteca de scripts de fecha y hora
Date.prototype.EseiseApyear jueces Año de salto
Date.prototype.Format Fecha de formato
Date.prototype.dataadd Cálculo de fecha
Date.prototype.datediff Compare la diferencia de fecha
Date.prototype.ToString fecha a la cadena
Date.prototype.toarray Fecha dividida en una matriz
Date.prototype.DatePart Obtenga información sobre la fecha
Date.prototype.maxdayofdate toma el número máximo de días en el mes en que se encuentra la fecha
Date.prototype.weeknumofyear determina la semana del año en que la fecha es
Cadena stringtodate hasta la fecha
ISValidDate Verificar la validez de la fecha
CheckDatetime Fecha y hora completa cheque
Días entre la diferencia de día de la fecha de la fecha
Código JS:
// ----------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------- dd/dd/d/d fecha // hh/hh/h/h tiempo // mm/m minutos // ss/ss/s/s Segundos // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------- % 100) .ToString (): '0' + (this.getyear () % 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 (/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; }Si desea implementar este reloj:
03 de diciembre de 2012 17:21:16 Lunes
// Función de reloj local calateOn () {var ahora = new Date (); var año = ahora.getTlyar (); // Getfulfyear getyear var mes = ahora.getMonth (); var fecha = ahora.getDate (); var day = ahora.getday (); var hora = ahora.gethours (); var minuto = ahora.getminutes (); var sec = ahora.getSeconds (); VAR Semana; mes = mes + 1; if (mes <10) mes = "0" + mes; if (fecha <10) fecha = "0" + fecha; if (hora <10) hora = "0" + hora; if (minu <10) minuto = "0" + minu; if (sec <10) Sec = "0" + Sec; var arr_week = nueva matriz ("domingo", "lunes", "martes", "jueves", "viernes", "sábado"); semana = arr_week [día]; var tiempo = ""; tiempo = año + "año" + mes + "mes" + fecha + "día" + "" + hora + ":" + minu + ":" + sec + "" + semana; $ ("#bgClock"). html (tiempo); VAR TIMER = SetTimeOut ("ClockOn ()", 200);}Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.