JS Processing Date

Source: Internet
Author: User

var mydate = new Date (); Mydate.getyear (); Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
Mydate.getmonth (); Get the current month (0-11, 0 for January)
Mydate.getdate (); Get current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
Mydate.gethours (); Get 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 (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time

Date-time Method extension

Date.prototype.isLeapYear Judging leap years

Date.prototype.isLeapYear = function () {return (0==this.getyear ()%4&& ((This.getyear ()%100!=0) | | (This.getyear ()%400==0)); }

Date.prototype.Format date Formatting

 1 Date.prototype.format = function (format) 2 {3                 var o = {4 "m+": This.getmonth () + 1,//month 5 "d+": This.getdate (),//day 6 "H +": this.gethours (),//hour 7 "m+": This.getminutes (),//minute 8 "s+": THIS.G Etseconds (),//second 9 "q+": Math.floor ((This.getmonth () + 3)/3),//quarter10 "S": Thi S.getmilliseconds ()//millisecond11}12 if (/(y+)/.test (format)) format = Format.replace (regexp.$1  , (This.getfullyear () + ""). substr (4-regexp.$1.length)), and the (var k in O) if (New RegExp (" + K + ")"). Test (format), format = Format.replace (regexp.$1,16 regexp.$1.length = = 1? o[k ]: ("XX" + o[k]). substr (("" + O[k]). (length)), return format;19}20 
   

Date.prototype.DateDiff Comparison Date Difference

  

Daysbetween Date Days Difference

DATE.PROTOTYPE.DATEADD Date Calculation

//+--------------------------------------------------- //| Date Calculation//+---------------------------------------------------Date.prototype.DateAdd = function (strinterval, number) { var dttmp = this; Switch (strinterval) {case ' s ': return new Date (Date.parse (dttmp) + (* 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 ());  } }

  

Date.prototype.toString Date to String

Date.prototype.toArray date split into arrays

  

Date.prototype.DatePart Partial information of the date taken

  

Date.prototype.MaxDayOfDate the maximum number of days in the month in which the date is taken

Date.prototype.WeekNumOfYear the week of the year in which the date is judged

//+--------------------------------------------------- //| Get the current date week is the week of the year//+---------------------------------------------------Date.prototype.WeekNumOfYear = function () {var mydate = this, var ary = Mydate.toarray (); var = 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; }

Stringtodate String to date type

Isvaliddate Verifying date Validity

Checkdatetime Full date and time check

  

Compatibility processing under Firefox

Firefox's getyear function gets the value of the current year minus 1900

var today=New Date ();

Date=today.getdate ();

Month=today.getmonth ();

month=month+1;

if (month<=9)

Month= "0" +month;

Year=today.getyear ();

if (year<1900) year+=1900; Compatibility processing under Firefox

JS Processing Date

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.