JS operation date (function, JS version of the DateAdd and DateDiff, rounding (Round () method) and retain the decimal point after the N-bit function) __ data structure

Source: Internet
Author: User
Tags date1 getdate

JS operation time (function)

var mydate = new Date (); Mydate.getyear (); Gets the current year (2-bit) mydate.getfullyear (); Get the complete year (4-bit, 1970-????) Mydate.getmonth (); Get the current month (0-11, 0 for January) mydate.getdate (); Gets the current day (1-31) mydate.getday (); Get Current week x (0-6, 0 representative Sunday) mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1) mydate.gethours (); Gets the current number of hours (0-23) mydate.getminutes (); Gets the current number of minutes (0-59) mydate.getseconds (); Gets the current number of seconds (0-59) mydate.getmilliseconds (); Gets the current number of milliseconds (0-999) mydate.tolocaledatestring (); Gets the current date var mytime=mydate.tolocaletimestring (); Gets the current time mydate.tolocalestring (); Get date and Time Date Time Script Library method list Date.prototype.isLeapYear judgment Leap year Date.prototype.Format date format Date.prototype.DateAdd Date calculation Date.prototype.DateDiff Comparison Date Difference Date.prototype.toString date-to-string Date.prototype.toArray date split array Date.prototype.DatePart Take a part of the date Date.prototype.MaxDayOfDate the maximum number of days of the month Date.prototype.WeekNumOfYear the date of the year Stringtodate string to date type Isvaliddate Validation Date validity checkdatetime Full date time check daysbetween date days difference JS code://------------------------------------------------ ---//judgment leap Year//---------------------------------------------------Date.prototype.isLeapYear = function () {return (0==this.getyear ()%4& & ((This.getyear ()%100!=0) | | (This.getyear ()%400==0)); }//---------------------------------------------------//Date format//Format YYYY/YYYY/YY/YY = year//mm/m month//w/w Week//dd/dd/ D/D Date//hh/hh/h/h time//mm/m minute//ss/ss/s/s seconds//---------------------------------------------------Date.prototype.Form at = function (formatstr) {var str = formatstr; var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; Str=str.replace (/yyyy| Yyyy/,this.getfullyear ()); Str=str.replace (/yy| yy/, (this.getyear ()%) >9? (This.getyear ()%). 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,week[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 (/hh| 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 ()); return str; } //+--------------------------------------------------- //| A two-time number of days difference date format is YYYY-MM-DD//+---------------------------------------------------function Daysbetween (Dateone, Datetwo) {var onemonth = 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.length,datetwo.lastindexof ('-') +1); var twoyear = datetwo.substring (0,datetwo.indexof ('-')); var cha=((Date.parse (onemonth+ '/' +oneday+ '/' +oneyear ')-date.parse (twomonth+ '/' +twoday+ '/' +twoyear '))/86400000); Return Math.Abs (CHA); } //+--------------------------------------------------- //| Date Calculation//+---------------------------------------------------Date.prototype.DateAdd = function (strinterval, number) { var dttmp = this; Switch (strinterval) {case ' s ': return new Date (Date.parse (dttmp) + (1000 * number)), Case ' n ': return new Date (Date.pars E (dttmp) + (60000 * number)); Case ' H ': return new Date (Date.parse (dttmp) + (3600000 * number)); Case ' d ': return new Date (Date.parse (dttmp) + (86400000 * number)); Case ' W ': Return new Date (Date.parse (dttmp) + ((86400000 * 7) * number)); Case ' Q ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number*3, Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ()); Case ' m ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number, dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ()); Case ' Y ': RETurn New Date ((dttmp.getfullyear () + number), Dttmp.getmonth (), Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), Dttmp.getseconds ()); } } //+--------------------------------------------------- //| Compare Date Difference dtend format is a date type or a valid date format string//+---------------------------------------------------Date.prototype.DateDiff = function (Strinterval, dtend) {var dtstart = this; if (typeof dtend = ' string ')//if the string is converted to date type {dtend = Stringtodate (DtE nd); Switch (strinterval) {case ' s ': Return parseint (Dtend-dtstart)/1000), Case ' n ': Return parseint ((Dtend-dtstart) /60000); Case ' H ': Return parseint ((dtend-dtstart)/3600000); Case ' d ': Return parseint ((dtend-dtstart)/86400000); Case ' W ': Return parseint (Dtend-dtstart)/(86400000 * 7)); Case ' m ': Return (Dtend.getmonth () +1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) *12)-(Dtstart.getmonth () +1); Case ' Y ': return dtend.getfullyear ()-dtstart.getfullyear (); } } //+--------------------------------------------------- //| Date output string, overloaded with the systemThe ToString method of the//+---------------------------------------------------Date.prototype.toString = function (Showweek) { var mydate= this; var str = mydate.tolocaledatestring (); if (Showweek) {var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' Six ']; STR + ' Week ' + week[mydate.getday ()];} return str; } //+--------------------------------------------------- //| Date legality Verification//| Format is: YYYY-MM-DD or yyyy/mm/dd//+---------------------------------------------------function isvaliddate (datestr) { var sdate=datestr.replace (/(^/s+|/s+$)/g, ""); Go to both sides of the space; if (sdate== ') return true; If the format satisfies the yyyy-(/) mm-(/) DD or yyyy-(/) m (/) DD or yyyy-(/) m (/) d or yyyy-(/) mm-(/) d is replaced with the '//database, the legal date can be: Yyyy-mm/dd (2003-3/ 21), the database is automatically converted to the YYYY-MM-DD format var s = sdate.replace (/[/d]{4,4}[/-/]{1}[/d]{1,2}[/-/]{1}[/d]{1,2}/g, ""); if (s== ')//description format satisfies YYYY-MM-DD or YYYY-M-DD or yyyy-m-d or yyyy-mm-d {var t=new Date (Sdate.replace, '/'); var ar = Sdate.split (/[-/:]/); if (Ar[0]!= t.getyear () | | ar[1]!= t.getmonth () +1 | | ar[2]!= t.getdate ()) {//alert (' wrong date format.The format is: Yyyy-mm-dd or YYYY/MM/DD. Notice leap year. '); return false; }} else {//alert (' wrong date format. The format is: Yyyy-mm-dd or YYYY/MM/DD. Notice leap year. '); return false; return true; } //+--------------------------------------------------- //| Date Time Check//| Format is: yyyy-mm-dd HH:MM:SS//+---------------------------------------------------function checkdatetime (str) {var reg = /^ (/d+)-(/d{1,2})-(/d{1,2}) (/d{1,2}):(/d{1,2}):(/d{1,2}) $/; var r = Str.match (reg); if (r==null) return false; R[2]=r[2]-1; var d= new Date (R[1],r[2],r[3],r[4],r[5],r[6]); if (D.getfullyear ()!=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; return true; } //+--------------------------------------------------- //| Split date into groups//+---------------------------------------------------Date.prototype.toArray = function () {var mydate = this ; var myarray = Array (); Myarray[0] = Mydate.getfullyear (); MYARRAY[1] = Mydate.getmonth (); MYARRAY[2] = Mydate.getdate (); MYARRAY[3] = mydate.gethours (); MYARRAY[4] = Mydate.getminutes (); MYARRAY[5] = Mydate.getseconds (); return myarray; } //+--------------------------------------------------- //| Get Date data Information//| The parameter interval represents the data type//| Y year M month d days W week WW Week h when N minute s sec//+---------------------------------------------------Date.prototype.DatePart = function (inter val) {var mydate = this; var partstr= '; var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; switch (interval) {case ' Y ':p artstr = Mydate.getfullyear (); Case ' m ':p artstr = Mydate.getmonth () +1;break; Case ' d ':p artstr = Mydate.getdate (); Case ' W ':p artstr = Week[mydate.getday ()];break; Case ' ww ':p artstr = Mydate.weeknumofyear (); Case ' h ':p artstr = mydate.gethours (); Case ' n ':p artstr = Mydate.getminutes (); Case ' s ':p artstr = Mydate.getseconds (); return partstr; } //+--------------------------------------------------- //| Gets the maximum number of days of the month in which the current date//+---------------------------------------------------Date.prototype.MaxDayOfDate = function () {var mydate = this; var ary = Mydate.toarray (); var date1 = (NE W Date (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 ')); return result; } //+--------------------------------------------------- //| Get the current date the week is the week ordinal of the year//+---------------------------------------------------Date.prototype.WeekNumOfYear = function () {var mydate = this; var ary = Mydate.toarray (), var year = ary[0], var month = ary[1]+1; var day = ary[2]; document.write (' < script language=vbscript/>/n '); document.write (' mydate = DateValue (' +month+ '-' +day+ '-' +year+ ')/n '); document.write (' result = DatePart (' ww ', mydate)/n '); document.write (' n '); return result; } //+--------------------------------------------------- //| string is converted to date type//| Format mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd//+---------------------------------------------------function Stringtodate (datestr) {var converted = Date.parse (DATESTR); var mydate = new Date (converted); if (isNaN (mydate)) {//var Delimcahar = datestr.indexof ('/')!=-1? ' /':'-'; var arys= datestr.split ('-'); MyDate = new Date (Arys[0],--arys[1],arys[2]); return mydate; } JS version of DateAdd and DateDiff

* The date of date, month, day, etc plus number of dates/Date.prototype.dateAdd = function (Interval,number) {var d = this; var k={' y ': ' fullyear ', ' Q ': ' Month ', ' m ': ' Month ', ' W ': ' Date ', ' d ': ' Date ', ' H ': ' Hours ', ' n ': ' Minutes ', ' s ': ' Seconds ', ' ms ': ' milliseconds '}; var n={' Q ': 3, ' W ': 7}; Eval (' D.set ' +k[interval]+ ' (D.get ' +k[interval]+ ' () + ' + ' + (n[interval]| | 1) (*number) + ') '); return D; }/* Calculates the date of difference between day and month/Date.prototype.dateDiff = function (interval,objdate2) {var d=this, i={}, T=d.gettime (), t2=objdate 2.getTime (); i[' y ']=objdate2.getfullyear ()-d.getfullyear (); i[' q ']=i[' y ']*4+math.floor (Objdate2.getmonth ()/4)-math.floor (D.getmonth ()/4); i[' m ']=i[' y ']*12+objdate2.getmonth ()-d.getmonth (); i[' Ms ']=objdate2.gettime ()-d.gettime (); i[' W ']=math.floor ((t2+345600000)/(604800000))-math.floor ((t+345600000)/(604800000)); i[' d ']=math.floor (t2/86400000)-math.floor (t/86400000); i[' h ']=math.floor (t2/3600000)-math.floor (t/3600000); i[' n ']=math.floor (t2/60000)-math.floor (t/60000); i[' s ']=math.floor (t2/1000)-math.floor (t/1000); Return I[interval]; }

DateAdd Method

Returns the Date object for which the specified time interval has been added.

Dateobj.dateadd (interval, number) Parameters

Dateobj

Required option. Any Date object.

Interval

Required option. A string expression that represents the time interval to add. For numeric values, see the "Settings" section.

Number

Required option. Numeric expression that represents the number of time intervals to add. Numeric expressions can be positive (get future dates) or negative numbers (get past dates). DateDiff Method

Returns the time interval between two date objects.

Dateobj.datediff (interval, dateObj2) Parameters

Interval

Required option. A string expression that is used to calculate the time interval between date1 and date2 . For numeric values, see the "Settings" section.

Dateobj, DateObj2

Required option. The Date object. The two date objects used for the calculation. Set up

The interval parameter can have the following values:

Set up Description
Y Years
Q Quarter
M Month
D Day
W Week
H Hours
N Minutes
S Seconds
Ms Milliseconds

JS Get Date Week

<mce:script language=javascript><!--//Today function Showtoday () {var nowdate=new Date (); M=number (Nowdate.getmonth ()) +1 return nowdate.getyear () + "-" +m+ "-" +nowdate.getdate (); ///First day of the week function Showweekfirstday () {var nowdate=new date (); var weekfirstday=new date (nowdate-(Nowdate.getday ()-1) * 86400000); return weekfirstday; //The last day of the week function Showweeklastday () {var nowdate=new date (); var weekfirstday=new date (nowdate-(Nowdate.getday ()-1) * 86400000); var weeklastday=new Date ((weekfirstday/1000+6*86400) *1000); return weeklastday; //The first day of the month function Showmonthfirstday () {var nowdate=new date (); var monthfirstday=new date (nowdate.getyear), Nowdate.getmonth (), 1); return monthfirstday; //Last day of this month function Showmonthlastday () {var nowdate=new date (); var monthnextfirstday=new date (Nowdate.getyear ()), Nowdate.getmonth () +1,1); var monthlastday=new Date (MonthNextFirstDay-86400000); return monthlastday; ///First day of last month function Showpreviousfirstday () {var monthfirstday=showmonthfirstday () RetuRN New Date (Monthfirstday.getyear (), Monthfirstday.getmonth () -1,1)}//Last month function Showpreviouslastday () {var Monthfirstday=showmonthfirstday (); return new Date (MonthFirstDay-86400000); ///First day of last week function Showpreviousfirstweekday () {var weekfirstday=showweekfirstday () return new Date ( WEEKFIRSTDAY-86400000*7)}//Last Last Day function Showpreviouslastweekday () {var weekfirstday=showweekfirstday () return new Date (WeekFirstDay-86400000)}//Last day function Showpreviousday () {var monthfirstday=new date (); return new Date (Monthfirst Day-86400000); //Next day function Shownextday () {var monthfirstday=new date (); return new Date ((monthfirstday/1000+86400) *1000);//first day of next week function Shownextfirstweekday () {var monthfirstday=showweeklastday () return new Date (monthfirstday/1000+86400) *1000 }///next week last day function Shownextlastweekday () {var monthfirstday=showweeklastday () return new Date (monthfirstday/1000+7* 86400) *1000)}//Next month first day function shownextfirstday () {var monthfirstday=showmonthfirstday () retUrn New Date (monthfirstday.getyear (), Monthfirstday.getmonth () +1,1)}//Last day of next month function Shownextlastday () {var Monthfirstday=showmonthfirstday () return new date (new Date (Monthfirstday.getyear (), Monthfirstday.getmonth () +2,1)- 86400000)} function Date.prototype.toString () {return this.getfullyear () + "-" + (This.getmonth () +1) + "-" +this.getdate ( ); } function setdate (num) {if (num==1) {document.all.datedate.value=showmonthfirstday (); document.all.datedate2.value= Showmonthlastday (); } if (num==2) {document.all.datedate.value=showweekfirstday (); Document.all.datedate2.value=showweeklastday ();} if ( num==3) {document.all.datedate.value=showtoday (); Document.all.datedate2.value=showtoday ();} if (num==4) { Document.all.datedate.value=showpreviousfirstday (); Document.all.datedate2.value=showpreviouslastday (); } if (num==5) {document.all.datedate.value=shownextfirstday (); Document.all.datedate2.value=shownextlastday ();} if ( num==6) {document.all.datedate.value=showpreviousfirstweekday (); Document.all.dateDAte2.value=showpreviouslastweekday (); } if (num==7) {document.all.datedate.value=shownextfirstweekday (); document.all.datedate2.value= Shownextlastweekday (); } if (num==8) {document.all.datedate.value=showpreviousday (); Document.all.datedate2.value=showpreviousday ();} if ( num==9) {document.all.datedate.value=shownextday (); Document.all.datedate2.value=shownextday ();}} --></mce:script> <input name=datedate type=text>-><input name=datedate2 type=text>   <input Name=haha Type=button value= "Last month" onclick=setdate (4) >   <input name=haha Type=button value= " This month "onclick=setdate (1) >  <input name=haha Type=button value=" next month "Onclick=setdate (5) >  <input Name=haha Type=button value= "Last week" onclick=setdate (6) >  <input name=haha Type=button value= "This Week" onclick= Setdate (2) >  <input name=haha Type=button value= "Next week" Onclick=setdate (7) >  <input Name=haha type =button value= "Last Day" Onclick=setdate (8) >  <input Name=haha Type=button value= "Today" onclick=setdate (3) >  <input name=haha Type=button value= "Next Day" Onclick=setdate (9) > can be flexible combination

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.