In JavaScript, the date object method is far less than that of other languages, and many common methods are not provided. The last time I used the calendar project, I added some methods, today, I sorted it out and wrote a datehelper object. I don't need to write it again next time.
VaR datehelper = {isleapyear: function (year) {// whether it is a leap year if (Year % 400 = 0) | (Year % 100! = 0) & (Year % 4 = 0) {return true;} else {return false ;}, daysinmonth: function (year, Mo) {// returns the number of days in the specified month if (mo = 2) {return this. isleapyear ()? 29: 28;} else {return (mo = 4 | Mo = 6 | Mo = 9 | Mo = 11 )? 30: 31 ;}, dayoffirstdateinmonth: function (year, Mo) {// returns the number of weeks on the first day of the specified month var newdate = new date (year, Mo-1 ); vaR day = newdate. getday (); return day;}, getclienttimezone: function () {// return the client Time Zone var d = new date (); Return-D. gettimezoneoffset ()/60 ;}}
These methods are commonly used, including determining whether it is a leap year, obtaining the number of days of a month, and obtaining the time zone settings of the client, the dayoffirstdateinmonth method is used in the calendar project to return the day of the week for the first day of a month to generate a calendar. In addition, using JavaScript to obtain the user's time zone settings is not the most secure method. At least in windows, when the system is set to some time zones, the Javascript gettimezoneoffset method cannot return the correct value. If you are interested, try it.
PS. Where the parameter Mo (month) is in the starting month of 1.
More methods will be updated later.
The following link contains the JS Code and demo. You are welcome to download it.
Date_helper.zip