Prefácio
No desenvolvimento do JavaScript front-end, geralmente encontramos o problema da data e hora do processamento. Muitas vezes usamos muitas funções de processamento para concluir um efeito simples de exibição de data e hora. Para melhorar a velocidade de desenvolvimento do site, você pode encapsular o código principal em uma função separada e chamá -la diretamente quando necessário. Vamos dar uma olhada em algum momento de processamento de aulas que você resumiu. Basta chamá -lo diretamente quando precisar.
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 o horário atual mydate.tolocalestring (); // obtenha a data e a hora do método da biblioteca de scripts date.prototype.isleApyear Juízes Data do ano do ano.Prototype.Format Formato Data.Prototype.dateadd Data de cálculo. Date.prototype.maxdayofdate Obtenha o número máximo de dias do mês em que a data é a data.prototype.weeknofyear determina a semana do ano em que a data é stringtodate string até a data do dia code://--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Judge leap year // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SS/SS/S/S. Segundos // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 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.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; } Para exibir: data atual mais horário (como: 2009-06-12 12:00) função currentTime () {var agora = new Date (); var ano = agora.getlyear (); // ano var mês = agora.getmonth () + 1; // mês var dia = agora.getDate (); // dia var hh = agora.gethours (); // tempo var mm = agora.getminutes (); // minuto var relógio = ano + "-"; if (mês <10) relógio += "0"; relógio + = mês + "-"; if (dia <10) relógio += "0"; relógio + = dia + ""; if (hh <10) relógio += "0"; relógio + = hh + ":"; if (mm <10) relógio += '0'; relógio += mm; retornar (relógio); }Resumir
O exposto acima é o conteúdo completo do resumo do processamento de tempo JavaScript. Espero que seja útil para todos no futuro usando JavaScript.