1. Description
This article is to read some good framework of the source code when you see, do a record here.
2.Specific content 2.1 Judging whether the year is a leap years 2.1.1 Preface
Why do you have a leap year?
In the case of the Sun, the "year" is based on a return to the year, and a return year is approximately 365.24220 days. Because in the common year West calendar only calculates 365 days, the result is four years later will accumulate 0.24220x4=0.9688 day, is about to wait for the date, therefore every four years increases Rinjita day to arrive at this 0.9688 day. However, 4 years after the accumulation of 0.96876 days, and the true 1st is still 0.03124 days, so if the changes in accordance with the 4-year 1 Rinjita, after a century will be accumulated into 365x100+25=36525 day, than the true 1 century day 365.24219x100= More than 36524.219. As a result, calendar jurists re-Rinjita the rules of the Year: the Year of 4 times Rinjita, 100 times the number of Rinjita, 400 times Rinjita. For example: A.D. 1992, 1996 and so on as 4 times, it is Rinjita year, A.D. 1800, 1900, 2100 as the times of 100, when the year is not Rinjita; A.D. 1600, 2000, 2400 for 400 times, so there are rinjita.
2.1.2 Rules
The year divided by 400 divisible, or the year divided by 4 divisible but divided by 100 is not divisible, for leap years.
2.1.3 Implementation
JS
function Isleapyear (year) {return ((year%4===0 && (year%100!==0)) | | (year%400===0));}
2.2. Get the number of days of a month in a given year 2.2.1 Rules The number of days in a certain month, the main difference is that 1,3,5,7,8,10,12 is 31 days of 4,6,9,11 is 30 days, February is to be based on whether it is a leap year depends on how many days, leap year 29 days, common year 28 days.
2.2.2 Implementation
JS
Using arrays and?:, the code is very concise function getdaysinmonth (year,month) {return [Isleapyear (year)? 29:28), 31,30,31,30,31,31,30,31,30,31][month];} Using the Swith statement feels like a cumbersome function getdaysinmonth (year,month) {switch (month) {case 1,3,5,7,8,10,12:return 31 ; Case 4,6,8,11:return 30; Case 2:return isleapyear (year)? 29:28; }}
Good code is like a good article is not overnight can write, also need to undergo several changes.
Code Concise Way (1)-year