Several extension methods for date objects in Javascript

Source: Internet
Author: User

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

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.