Este artigo compartilha com você a data atual e a operação de formatação do JS. O conteúdo específico é o seguinte
var mydate = new Date (); mydate.getyear (); // Obtenha o ano atual (2 dígitos) mydate.getlyear (); // Obtenha o ano inteiro (4 dígitos, 1970-????) mydate.getmonth (); // Obtenha o mês atual (0-11, 0 representa janeiro) mydate.getdate (); // Obtenha o dia atual (1-31) mydate.getday (); // Obtenha a semana atual x (0-6, 0 representa domingo) mydate.gettime (); // Obtenha a hora atual (milissegundos a partir de 1970.1.1) mydate.gethours (); // Obtenha o número atual de horas (0-23) mydate.getminutes (); // Obtenha o número atual de minutos (0-59) mydate.getSeconds (); // Obtenha o número atual de segundos (0-59) mydate.getMillisEconds (); // Obtenha o número atual de milissegundos (0-999) mydate.tolocaledateString (); // obtenha a data atual var mytime = mydate.tolocaletimestring (); // obtenha o horário atual mydate.tolocalestring (); // obtenha o horário atual mydate.tolocalestring (); // Obtenha a data e a hora
Lista de métodos de biblioteca de scripts de data e hora
DATE.PROTOTYPE.ILLEAPYEAR JUINTES ANO GRALIDO
Date.prototype.Format Data Formating
DATE.PROTOTYPE.DATADD Cálculo da data
Date.prototype.datediff Compare a diferença de data
Data.prototype.ToString Data para string
Date.prototype.toArray Data dividida em matriz
Date.prototype.datePart Obtenha algumas informações sobre a data
Date.prototype.maxdayofdate leva o número máximo de dias no mês em que a data está localizada
Date.prototype.weekknofyear determina a semana do ano em que a data é
String stringtodate até o momento
IsValiddate Verifique a validade da data
Verifique a data e a hora da data e hora da CheckDateTime
Dias entre a diferença de dia de data
Código JS:
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- dd/dd/d/d data // hh/hh/h/h tempo // mm/m minutos // ss/ss/s/s/s Segundos // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- % 100) .ToString (): '0' + (this.getyear () % 100)); str = str.place (/mm/ ,this.getMonth ()> 9? this.getMonth (). ToString (): '0' + this.getMonth ()); str = str.Replace (/m/g,This.getmonth ()); str = str.place (/w | w/g, semana [this.getday ()]); str = str.Place (/dd|dd/ ,this.getdate ()> 9? this.getdate (). tostring (): '0' + this.getdate ()); str = str.Replace (/d|d/g,This.getDate ()); str = str.Place (/hh|hh/, this.gethours ()> 9? this.gethours (). ToString (): '0' + this.gethours ()); str = str.Replace (/h|h/g,This.gethours ()); str = str.place (/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.Place (/ss|ss/, this.getSeconds ()> 9? this.getSeconds (). ToString (): '0' + this.getSeconds ()); str = str.Replace (/s|s/g,This.getSeconds ()); retornar str; } //+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- DateOne.substring (5, dateOne.LastIndexOf ('-')); var oneDay = dateOne.subString (dateOne.Length, dateOne.LastIndexOf ('-')+1); var Oneyear = dateOne.substring (0, dateOne.IndexOF ('-')); var Twomonth = DATETWO.SUBSTRING (5, DATETWO.LASTIndexOf ('-')); var Twoday = DATETWO.SUBSTRING (DATETWO.LENGLEN, DATETWO.LASTIndexOF ('-')+1); var Twoyear = DATETWO.SUBSTRING (0, DATETWO.Indexof ('-')); var cha = ((date.parse (onemonth+'/'+oneday+'/'+Oneyear)- date.parse (twomonth+'/'+twoday+'/'+twoyear))/86400000); retornar math.abs (cha); } //+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- switch (strInterval) {case 's': retornar nova data (data.parse (dttmp) + (número 1000 *)); case 'n': retornar nova data (data.parse (dttmp) + (número 60000 *)); case 'h': retornar nova data (data.parse (dttmp) + (número 3600000 *)); case 'd': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + (86400000 * número)); case 'w': retornar nova data (data.parse (dttmp) + ((86400000 * 7) * número)); Caso 'q': retorne a nova data (dttmp.getlyear (), (dttmp.getmonth ()) + número*3, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds (); Caso 'm': retorne a nova data (dttmp.getlyear (), (dttmp.getmonth ()) + número, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds ()); Caso 'y': retorne a nova data ((dttmp.getlyear () + número), dttmp.getmonth (), dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getSeconds ()); }} //+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------ //+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 60000); Caso 'H': Return Parseint ((DTEND - DTSTART) / 3600000); case 'd': retornar parseint ((dtend - dtstart) / 86400000); case 'W': Return Parseint ((dtend - dtstart) / (86400000 * 7)); case 'm': return (dtend.getmonth ()+1)+((dtend.getlyear () - dtstart.getlyear ())*12) - (dtstart.getMonth ()+1); case 'y': return dtend.getlyear () - dtstart.getlyear (); }} //+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + = 'Semana' + semana [mydate.getday ()]; } retornar 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 converter automaticamente em formato yyyy-mm-dd var s = sdate.replace (/[/d] {4,4} [/-/] {1} [/d] {1,2} [/-] {1} [/d] {1,2}/g, ''); if (s == '') // O formato de instrução satisfaz aaaa-mm-dd ou aaaaa-m-dd ou aaaa-md ou aaaaa-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 data errada! retornar falso; }} else {// alert ('formato de data errada! O formato é: yyyy-mm-dd ou yyyy/mm/dd. Observe o ano da folha.'); retornar falso; } retornar true; } //+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- }) $/; var r = str.match (reg); if (r == null) retorna false; r [2] = r [2] -1; var d = nova data (r [1], r [2], r [3], r [4], r [5], r [6]); if (d.getlyear ()! = r [1]) retornar false; if (d.getmonth ()! = r [2]) retornar false; if (d.getdate ()! = r [3]) retornar false; if (d.gethours ()! = r [4]) retorna false; if (d.getminutes ()! = r [5]) retornar false; if (d.getSeconds ()! = r [6]) retorna false; retornar true; } //+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ myarray [3] = mydate.gethours (); myarray [4] = mydate.getminutes (); myarray [5] = mydate.getSeconds (); retornar Myarray; } //+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------ = ['dia', 'um', 'dois', 'três', 'quatro', 'cinco', 'seis']; switch (intervalo) {case 'y': partestr = mydate.getlyear (); quebra; case 'm': partestr = mydate.getmonth ()+1; quebra; case 'd': partestr = mydate.getdate (); quebra; case 'w': partestr = semana [mydate.getday ()]; quebra; case 'ww': partestr = mydate.weeknofyear (); quebra; case 'h': partestr = semana [mydate.getday ()]; quebra; case 'ww': partestr = mydate.weeknofyear (); quebra; case 'h': partestr = mydate.gethours (); quebra; case 'n': partestr = mydate.getminutes (); quebra; case 's': partestr = mydate.getSeconds (); quebra; } retornar parte PartStr; } //+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (nova data (ary [0], ary [1] +1,1)); var date2 = date1.dateadd (1, 'm', 1); var resultado = datediff (date1.format ('yyyy-mm-dd'), date2.format ('yyyy-mm-dd')); resultado de retorno; } //+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- mydate.toArray (); var ano = ary [0]; var mês = ary [1] +1; var dia = ary [2]; document.write ('<idioma script = vbscript/n'); document.write ('mydate = datu (' '+mês+'-'+dia+'-'+ano+' ') /n'); document.write ('resultado = datePart (' ww ', mydate) /n'); document.write (' /n'); resultado de retorno; } //+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Formato mm/dd/yyyy mm-dd-yyyyyyyyy/mm/dd yyyy-mm-dd //+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Data (arys [0],-arys [1], arys [2]); } retornar mydate; }Se você deseja implementar esse relógio:
03 de dezembro de 2012 17:21:16 Segunda -feira
// Função do relógio local clockon () {var agora = new Date (); var ano = agora.getlyear (); // obtrinalmente getyear var mês = agora.getmonth (); var date = agora.getDate (); var dia = agora.getday (); var hour = agora.gethours (); var minuto = agora.getminutes (); var sec = agora.getSeconds (); Var Week; mês = mês + 1; if (mês <10) mês = "0" + mês; if (data <10) date = "0" + data; if (hora <10) hora = "0" + hora; if (minu <10) minuto = "0" + minu; if (seg <10) sec = "0" + sec; var Arr_week = New Array ("Sunday", "Segunda -feira", "Terça -feira", "Quinta -feira", "Friday", "Sábado"); Semana = arr_week [dia]; var time = ""; tempo = ano + "ano" + mês + "mês" + data + "dia" + "" + hora + ":" + minu + ":" + sec + "" + semana; $ ("#bgclock"). html (tempo); var timer = setTimeout ("Clockon ()", 200);}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.