Contacts

How many months passed between two dates. How is one mathematical formula by the number of the month to calculate the number of days in it? How many days between dates? Instruction

Note: This post is the translation of the article cmcenroe.me/2014/12/05/days-in-month-formula.html ( Part I.), as well as copyrighted add-on ( Part II.). Should not be treated seriously, but rather as a warm-up for the mind, requiring no more than school knowledge of arithmetic and having no practical application. All pleasant reading!

Part I.

Introduction

Recently, after another sleepless night, I thought about the methods of memorizing the number of days in every month of the year. To do this, there is a count, as well as the way to consider the fingers on the knuckles, but neither one nor the other arranged me. I thought about, and whether there is any mathematical formula to solve such a task, and without finding it when I triggered the study - I threw a challenge to create it.

Formalizing in other words, it is necessary to find a feature. f.such that value f (X) For each month x.represented by a number from 1 to 12 equals the number of days this month. Table of argument and function values:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 28 31 30 31 30 31 31 30 31 30 31

If you have a desire to try yourself before reading my decision, now it's time. If you prefer to immediately see the ready-made answer, then look under the spoiler.

Answer


Below are my steps to find a solution.

Mathematical apparatus

First, we were drunk in memory two vital in solving this task of the operator: integer division and balance from division.

Integer divisionthis is an operator used in many programming languages \u200b\u200bwhen dividing two integers and discard from private fractional part. I will depict it as. For example:

Remainder of the division This is an operator that is a led from division. In many programming languages, a symbol is applied % , I will use the design of the type, for example:

I note that the balance of division has an equal priority with division.

Basics

So, we apply our mathematical apparatus to obtain the basic formula. As a normal month, 30 or 31 days, so we can use to obtain alternately 1 or 0, and then simply add to this number the constant:

We receive the table, the correct values \u200b\u200bare allocated:
x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 30 31 30 31 30 31 30 31 30 31 30

Good start! There are already the correct values \u200b\u200bfor January and for months from March to July inclusive. February is a special case, and we will deal with him a little later. After July, for the remaining months, the order of obtaining 0 and 1 must be changed to the opposite.
For this, we can add to Delimo 1:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 30 31 30 31 30 31 30 31 30 31 30 31

Now the correct values \u200b\u200bfrom August to December, but, as was supposed, the values \u200b\u200bfor other months are incorrect. Let's see how we can combine these formulas.

Mask overlay

To do this, a piecewise specified function is needed, but - since it seemed to me that it seemed to me - I thought about another solution of solutions using one part of the function on the same interval, the other on the other.
I believe that the easiest way to find an expression equal to 1 in one application area and 0 in the rest. The method in which there is a multiplying argument to the expression we exclude it from the formula outside the field of its use, I called "the imposition of a mask", therefore such behavior is similar to some bit mask.
To apply this method, in the last part of our function, it is necessary to find an expression equal to 1 at, and - since the values \u200b\u200bof the argument are always less than 16 - for this purpose is perfectly suitable for an integer division by 8.
x. 1 2 3 4 5 6 7 8 9 10 11 12
x. ⁄ 8 ⌋ 0 0 0 0 0 0 0 1 1 1 1 1

Now with this mask, using expression in division instead of 1, we can replace the formulation of 0 and 1 formula to be reverse:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 30 31 30 31 30 31 31 30 31 30 31

Eureka! That's right, except February. Surprise surprise.

February

In any month, 30 or 31 days, except for February, with its 28 (leap year, go beyond this task). At the moment, according to our formula in it, 30 days, so it would be nice to subtract an expression equal to 2 at.
The best that I managed to come up with it, which imposes a mask for all months after February:
x. 1 2 3 4 5 6 7 8 9 10 11 12
2 mod. x. 0 0 2 2 2 2 2 2 2 2 2 2

By changing the basic constant by 28 with the addition of 2 to the rest of the month we will get the formula:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 29 28 31 30 31 30 31 31 30 31 30 31

Unfortunately, January is now shorter for 2 days. But, fortunately, to get an expression that will be applied only for the first month is very easy: it is rounded down the opposite. Multiplying it on 2 we get the final formula:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 28 31 30 31 30 31 31 30 31 30 31

Afterword

Here it is a formula for obtaining the number of days in any month of the year, using the simplest arithmetic. The next time when you remember how many days in September, simply perform with this single-line function on JavaScript:

FUNCTION F (X) (RETURN 28 + (X + Math.floor (X / 8))% 2 + 2% x + 2 * Math.floor (1 / x);)

Part II.

Introduction

In the first part, a short and even slightly elegant formula was obtained, the main advantages of which are the simplicity of the mathematical apparatus, the lack of branches and conditional expressions, conciseness. The disadvantages are in addition that you will not apply it in your project - you can include the lack of checking for a viscous and not leap year.
So I set a task to create a function f.such that value f (x, y) For each month x.represented by the number from 1 to 12 and year y., more 0, equals the number of days in the month x. in year y..
For impatient under the spoiler, there is a ready-made answer, I ask the rest to follow me.

Answer

Remainder of the division: mod. and ⌊⌋

For visual clarity, we agree that in some formulas, the division operator with the residue is replaced by lower brackets, where it seemed necessary to me:

Leap year

An additional calendar day is introduced into the leap year: February 29. As is known, the leap is a year, multiple 4 and not a multiple 100, or a multiple of 400. We write an expression identical to this statement:

To bring this expression to algebraic, it is necessary to apply to the result of an expression injection of the form:

What will allow obtaining 1 when dividing without a residue and 0 when dividing with the residue to use it in the formula for determining the number of days in a month.

As a function g " You can use 1 minus balance from division for:

x. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g "(x) Infinity. 1 0 0 0 0 0 0 0 0 0 0 0 0 0

It is easy to notice that by increasing the divide and divider on 1 we will get the correct formula when:
x. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g "(x) 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Thus, the expression will write as:


And write the expression as:

Applying this approach we obtain the following function. g (Y), whose value will be 1 if the year is leap, or 0 in the opposite case:

y. 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
g (Y) 0 0 1 0 0 0 1 0 0 0 1
y. 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000
g (Y) 1 0 0 0 1 0 0 0 1 0 0

The biliary year was isolated.

I remind you that in the framework of the adopted agreement, the operator of obtaining a balance from division can be depicted as mod.and ⌊⌋.

Mask overlay

In the formula, part is an amendment that adds 2 days to January. If you remove the multiplier 2 and in the numerator to replace 1 on 2, then this formula will add 2 days to January and 1 day by February, which gives us the key to the addition of the day in the leap year. For clarity, use in the formula intermediate value g (Y) and in quality y. We use 2000 (leaps) and 2001 (not leap) years:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f (X, 2001) 30 28 31 30 31 30 31 31 30 31 30 30

Values \u200b\u200bfor all months, except for January, not a leap year is true.

To correct this annoying misunderstandings, add to January 1 day already known to us by the formula:


Or:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f (X, 2001) 31 28 31 30 31 30 31 31 30 31 30 30

Conclusion

As a result, it is already obtained significantly more cumbersome, but a more universal formula, which can also be used to obtain the number of days in a month of a certain year:

FUNCTION F (X, Y) (RETURN 28 + ((X + Math.floor (X / 8))% 2) + 2% x + math.floor ((1 + (1 - (y% 4 + 2)% (y% 4 + 1)) * (((y% 100 + 2)% (y% 100 + 1)) + (1 - (y% 400 + 2)% (y% 400 + 1))) / x) + Math.floor (1 / x) - math.floor (((1 - (y% 4 + 2)% (y% 4 + 1)) * ((y% 100 + 2)% (y% 100 + 1 )) + (1 - (y% 400 + 2)% (y% 400 + 1))) / x);)
Example on C # ideone.com/Fanutz.

1 . I do not know how to use a similar mnemonic, so I spied the tablet on the Internet.
2 . "Basics", or "Rule with many exceptions", like most rules.
3 . Initially, in the Roman calendar, February was the last month of the year, so there is logic that it is shorter than everyone else. There is also logic in adding or removing the day at the end of the year, so its length is a variable.

UPD. one:
Alternative translation of the first part in

Free Online Contour Calculator. The Buchelli will help you and tell you how many days have passed between the two specified dates. In addition, if you have a need, you can calculate how many calendar, weekends or working days (hours) contains the specified period of the year or several years.

How many days between dates? Instruction

You just ask a specific day of the beginning and end and get the calculation through the share of seconds. All data online Calculator believes independently. If you change the starting days of the week, the result is automatically recalculated, taking into account the leap year.

Important: It is impossible to take from the calculations over the past year indicators of working days / hours per month and provide as calculations - the data will be different. Therefore, it is better to use the calculator.

So, the procedure:

  1. In the fields "Elementary Date" and "final date" choose the initial and end day of the reference, starting from 2013 and ending in the future 2018.
  2. Install the number of working hours in the next field. By default, this field is already 8 hours (40-hour work week), but you can change this figure.
  3. On the right side of the screen on the banner you will see the result obtained: working days, calendar days and working hours between specified dates. The results must be copied and saved in your document.

For what can be used calculator

  1. To calculate penalties and overdue under contracts
  2. How to understand the effectiveness of the use of some resource and limit deadlines
  3. How accidentally not to assign the timing of the task of the weekend
  4. How much time left to Deadline

Example:

You are an accountant. The head asked you in the next couple of minutes to provide data on the number of working hours, which all employees of the company in February should work out. The number of workers you can easily determine - you have numbers before your eyes. But the number of hours should be considered .... and how many days in February? And the year is a leap? And what days were weekends? How to determine the number of days of holidays?

Solution: Just use our widget. All information you will receive automatically, you do not need desktop calendars and calculators.

Did you like this calculator? Then try our other opportunities

Want to keep accounting, send reporting and make calculations in a convenient and simple web service? Trying free 14 days outline. Buchetter! We will quickly teach you how to use the service and answer all the questions!

To solve some tasks, when creating a table, you need in a separate cell or inside the formula, specify the number of days in a month so that the program should have the necessary calculations. Excel has tools intended for performing this operation. let's consider various methods Applications of this feature.

Calculate the number of days in a month in Excele using special category operators "Date and time". To find out exactly which option is best to apply, you need, first of all, set the goals of the operation. Depending on this, the result of the calculation may be discharged into a separate element on a sheet, and can also be used inside another formula.

Method 1: Combination of Operators Day and Conmines

Most simple way Solve this task is a combination of operators DAY and Konmena.

Function DAY belongs to the group of operators "Date and time". It indicates a specific number from 1 before 31 . In our case, the task this operator There will be an indication last day months with the built-in function in the form of an argument Konmena.

Syntax operator DAY following:

Day (date_v_)

That is, the only argument of this feature is "Date in numerical format". It will ask the operator Konmena. It must be said that the date in the numerical format differs from the usual format. For example, the date 04.05.2017 in a numeric form will look like 42859 . Therefore, this format excel uses only for internal operations. It is rarely used to display in cells.

Operator Konmena Designed to indicate the sequence number of the last day of the month, which is located on the specified number of months in advance or back from the specified date. The syntax function is:

Conmines (Nach_Data; number_mets)

Operator "Elementary Date" Contains a date from which the countdown is made or a link to the cell, where it is located.

Operator "Number of months" Indicates the number of months to which the countdown should be countdown.

Now let's see how it works on specific example. To do this take excel sheetTo one of the cells of which a certain calendar number is inscribed. It is necessary using the above set of operators to determine how many days in the monthly period to which this number refers.


General formula We took the following form:

Day (convince (b3; 0))

In this formula, the variable value is only the cell address ( B3.). Thus, if you do not want to perform the procedure through Masters functions, You can insert this formula into any sheet element, simply replacing the cell address containing the number to one that is relevant in exactly your case. The result will be similar.

Method 2: Automatic Determination of the number of days

Now let's look at another task. It is required that the number of days is not displayed at a given calendar number, but on the current. In addition, the change of periods would be carried out automatically without user participation. Although it seems strange, but this task is easier than the previous one. To solve it, even open Master of Functions No need, since the formula that performs this operation does not contain variables or links to cells. You just can drive into the cell of the sheet where you want to display the result, the following formula unchanged:

Day (Devil (today (); 0))

The built-in function today, which we applied in this case displays today's number and has no arguments. Thus, in your cell, the number of days in the current month will be displayed.

Method 3: Calculation of the number of days for use in complex formulas

In the examples above, we showed how to perform the calculation of the number of days in a month along the specified calendar number or automatically at the current month with the output of the result in a separate cell. But finding this value may be needed to calculate other indicators. At the same time, the calculation of the number of days will be carried out inside the complex formula and will not be displayed in a separate cell. Let's see how to do this on the example.

We need to make so that the number of days left until the end of the current month will be displayed in the cell. As in the previous method, this option does not require opening Masters functions. You can simply drive into the cell the following expression:

Day (Conmines (today (); 0)) - Day (today ())

After that, the number of days until the end of the month will be displayed in the specified cell. Every day the result will be automatically updated, and from the beginning of the new period, the count will begin again. It turns out a kind of countdown timer.

As we see, the specified formula consists of two parts. The first of them is an already familiar expression of the calculation of the number of days in a month:

Day (Devil (today (); 0))

But in the second part, it is subtracted from this indicator of today:

Day (today ())

Thus, when performing this calculation, the formula for calculating the number of days is part of more complex formula.

Method 4: Alternative Formula

But, unfortunately, the versions of the program earlier excel 2007 is missing operator Konmena. How to be the users who use old versions of the application? For them, such an opportunity exists through another formula, which is more massive than described above. Let's see how to calculate the number of days in a month along the specified calendar number using this option.

  1. Select the cell to output the result and go to the operator arguments window DAY Already familiar to us. Install the cursor into the only field of this window and click on the inverted triangle to the left of the formula string. Go to section "Other functions ...".
  2. In the window Masters functions in a group "Date and time" Allocate the name "DATE" and click on the button Ok.
  3. The operator window is launched DATE. This feature converts the date from the usual format to numeric valuewhich will then be then handled the operator DAY.

    The window that opened has three fields. In field "Day" You can immediately enter the number "one". It will be an unchanged action for any situation. But two other fields will have to do thoroughly.

    Install the cursor in the field "Year". Next, go to the selection of operators through a triangle familiar to us.

  4. All in the same category Masters functions Allocate the name "YEAR" and click on the button Ok.
  5. The operator arguments window starts YEAR. It defines the year at the specified number. In the only window of the window "Date in numerical format" Indicate a link to a cell containing the starting date for which you need to determine the number of days. After that, do not rush click on the button Ok, and click on name "DATE" in the formula row.
  6. Then we return back to the argument window DATE. Install the cursor in the field "Month" and go to the choice of functions.
  7. IN Wizard functions Click by name "MONTH" and click on the button Ok.
  8. The function arguments window starts MONTH. Its tasks are similar to the previous operator, only it displays the number of the month. In the only field of this window, we set the same reference to the original number. Then in the formula row click by name "DAY".
  9. Return to the argument window DAY. Here we have to do only one small barcode. In the single window of the window, in which the data is already located, add an expression to the end of the formula "-one" without quotes, as well as set "+1" after the operator MONTH. After that click on the button Ok.
  10. As we can see, in the pre-selected cell, the number of days in the month, to which the specified number belongs. The general formula has this kind:

    Day (date (year (D3); month (D3) +1; 1) -1)

The secret of this formula is simple. We will determine the date of the next day of the next period, and then take one day from it, receiving the number of days in the specified month. Variable magnitude in this formula is a link to the cell D3. in two places. If you replace it with the address of the cell in which the date is in your particular case, you can simply drive this expression to any element of the sheet without help. Masters functions.

As you can see, there are several options to find out the number of days in a month in Excel. What exactly to use depends on the ultimate goal of the user, as well as from what version of the program it uses.

The dates calculator is designed to calculate the number of days between dates, as well as to find the date by adding or subtracting a certain number of days to the known date.

Add days to date

In order to find out what number will be through a certain number of days, use this option. Enter the starting date and the number of days to add to it. For subtraction, use the value with a minus. The calculator also has an option for adding only working days.

Calculating the number of days between dates

This calculation method will answer the question "how many days have passed from the date." Enter the start date and end date and click the "Calculate" button. Calculator will show how many days between the dates entered. Separately calculator will show the number of working days.

With this option, you can calculate how many days left to a certain event, for example, before the birthday or holiday. To do this, in the initial date field, specify the current number, and in the end date field - the date of events.

Holidays

The calculator can calculate, add and deduct both calendar days and workers. Official non-working festive days are:

  • 1,2,3,5,6,8 January - New Year holidays
  • January 7 - Orthodox Christmas
  • February 23 - Defender of the Fatherland Day
  • March 8 - International Women's Day
  • May 1 - Spring and Labor holiday
  • May 9 - Victory Day
  • June 12 - Russian Day
  • November 4 - Day of People's Unity

If the festive day fell on Saturday or Sunday, he is transferred for the nearest working day. But sometimes the weekend is transferred to another place of the calendar. For example, Saturday and Sunday, who fell on New Year's holidays, can be transferred to May to extend the May holidays.

Calculator when calculating days takes into account both official festive dates and all transfers.

Note: This post is the translation of the article cmcenroe.me/2014/12/05/days-in-month-formula.html ( Part I.), as well as copyrighted add-on ( Part II.). Do not relate to the material seriously, but rather as a warm-up for the mind that requires no more than school knowledge of arithmetic and not practical application. All pleasant reading!

Part I.

Introduction

Recently, after another sleepless night, I thought about the methods of memorizing the number of days in every month of the year. To do this, there is a count, as well as the way to consider the fingers on the knuckles, but neither one nor the other arranged me. I thought about, and whether there is any mathematical formula to solve such a task, and without finding it when I triggered the study - I threw a challenge to create it.

Formalizing in other words, it is necessary to find a feature. f.such that value f (X) For each month x.represented by a number from 1 to 12 equals the number of days this month. Table of argument and function values:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 28 31 30 31 30 31 31 30 31 30 31

If you have a desire to try yourself before reading my decision, now it's time. If you prefer to immediately see the ready-made answer, then look under the spoiler.

Answer


Below are my steps to find a solution.

Mathematical apparatus

First, we were drunk in memory two vital in solving this task of the operator: integer division and balance from division.

Integer divisionthis is an operator used in many programming languages \u200b\u200bwhen dividing two integers and discarding from a private fractional part. I will depict it as. For example:

Remainder of the division This is an operator that is a led from division. In many programming languages, a symbol is applied % , I will use the design of the type, for example:

I note that the balance of division has an equal priority with division.

Basics

So, we apply our mathematical apparatus to obtain the basic formula. As a normal month, 30 or 31 days, so we can use to obtain alternately 1 or 0, and then simply add to this number the constant:

We receive the table, the correct values \u200b\u200bare allocated:
x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 30 31 30 31 30 31 30 31 30 31 30

Good start! There are already the correct values \u200b\u200bfor January and for months from March to July inclusive. February is a special case, and we will deal with him a little later. After July, for the remaining months, the order of obtaining 0 and 1 must be changed to the opposite.
For this, we can add to Delimo 1:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 30 31 30 31 30 31 30 31 30 31 30 31

Now the correct values \u200b\u200bfrom August to December, but, as was supposed, the values \u200b\u200bfor other months are incorrect. Let's see how we can combine these formulas.

Mask overlay

To do this, a piecewise specified function is needed, but - since it seemed to me that it seemed to me - I thought about another solution of solutions using one part of the function on the same interval, the other on the other.
I believe that the easiest way to find an expression equal to 1 in one application area and 0 in the rest. The method in which there is a multiplying argument to the expression we exclude it from the formula outside the field of its use, I called "the imposition of a mask", therefore such behavior is similar to some bit mask.
To apply this method, in the last part of our function, it is necessary to find an expression equal to 1 at, and - since the values \u200b\u200bof the argument are always less than 16 - for this purpose is perfectly suitable for an integer division by 8.
x. 1 2 3 4 5 6 7 8 9 10 11 12
x. ⁄ 8 ⌋ 0 0 0 0 0 0 0 1 1 1 1 1

Now with this mask, using expression in division instead of 1, we can replace the formulation of 0 and 1 formula to be reverse:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 30 31 30 31 30 31 31 30 31 30 31

Eureka! That's right, except February. Surprise surprise.

February

In any month, 30 or 31 days, except for February, with its 28 (leap year, go beyond this task). At the moment, according to our formula in it, 30 days, so it would be nice to subtract an expression equal to 2 at.
The best that I managed to come up with it, which imposes a mask for all months after February:
x. 1 2 3 4 5 6 7 8 9 10 11 12
2 mod. x. 0 0 2 2 2 2 2 2 2 2 2 2

By changing the basic constant by 28 with the addition of 2 to the rest of the month we will get the formula:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 29 28 31 30 31 30 31 31 30 31 30 31

Unfortunately, January is now shorter for 2 days. But, fortunately, to get an expression that will be applied only for the first month is very easy: it is rounded down the opposite. Multiplying it on 2 we get the final formula:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X) 31 28 31 30 31 30 31 31 30 31 30 31

Afterword

Here it is a formula for obtaining the number of days in any month of the year, using the simplest arithmetic. The next time when you remember how many days in September, simply perform with this single-line function on JavaScript:

FUNCTION F (X) (RETURN 28 + (X + Math.floor (X / 8))% 2 + 2% x + 2 * Math.floor (1 / x);)

Part II.

Introduction

In the first part, a short and even slightly elegant formula was obtained, the main advantages of which are the simplicity of the mathematical apparatus, the lack of branches and conditional expressions, conciseness. The disadvantages are in addition that you will not apply it in your project - you can include the lack of checking for a viscous and not leap year.
So I set a task to create a function f.such that value f (x, y) For each month x.represented by the number from 1 to 12 and year y., more 0, equals the number of days in the month x. in year y..
For impatient under the spoiler, there is a ready-made answer, I ask the rest to follow me.

Answer

Remainder of the division: mod. and ⌊⌋

For visual clarity, we agree that in some formulas, the division operator with the residue is replaced by lower brackets, where it seemed necessary to me:

Leap year

An additional calendar day is introduced into the leap year: February 29. As is known, the leap is a year, multiple 4 and not a multiple 100, or a multiple of 400. We write an expression identical to this statement:

To bring this expression to algebraic, it is necessary to apply to the result of an expression injection of the form:

What will allow obtaining 1 when dividing without a residue and 0 when dividing with the residue to use it in the formula for determining the number of days in a month.

As a function g " You can use 1 minus balance from division for:

x. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g "(x) Infinity. 1 0 0 0 0 0 0 0 0 0 0 0 0 0

It is easy to notice that by increasing the divide and divider on 1 we will get the correct formula when:
x. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g "(x) 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Thus, the expression will write as:


And write the expression as:

Applying this approach we obtain the following function. g (Y), whose value will be 1 if the year is leap, or 0 in the opposite case:

y. 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
g (Y) 0 0 1 0 0 0 1 0 0 0 1
y. 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000
g (Y) 1 0 0 0 1 0 0 0 1 0 0

The biliary year was isolated.

I remind you that in the framework of the adopted agreement, the operator of obtaining a balance from division can be depicted as mod.and ⌊⌋.

Mask overlay

In the formula, part is an amendment that adds 2 days to January. If you remove the multiplier 2 and in the numerator to replace 1 on 2, then this formula will add 2 days to January and 1 day by February, which gives us the key to the addition of the day in the leap year. For clarity, use in the formula intermediate value g (Y) and in quality y. We use 2000 (leaps) and 2001 (not leap) years:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f (X, 2001) 30 28 31 30 31 30 31 31 30 31 30 30

Values \u200b\u200bfor all months, except for January, not a leap year is true.

To correct this annoying misunderstandings, add to January 1 day already known to us by the formula:


Or:

x. 1 2 3 4 5 6 7 8 9 10 11 12
f (X, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f (X, 2001) 31 28 31 30 31 30 31 31 30 31 30 30

Conclusion

As a result, it is already obtained significantly more cumbersome, but a more universal formula, which can also be used to obtain the number of days in a month of a certain year:

FUNCTION F (X, Y) (RETURN 28 + ((X + Math.floor (X / 8))% 2) + 2% x + math.floor ((1 + (1 - (y% 4 + 2)% (y% 4 + 1)) * (((y% 100 + 2)% (y% 100 + 1)) + (1 - (y% 400 + 2)% (y% 400 + 1))) / x) + Math.floor (1 / x) - math.floor (((1 - (y% 4 + 2)% (y% 4 + 1)) * ((y% 100 + 2)% (y% 100 + 1 )) + (1 - (y% 400 + 2)% (y% 400 + 1))) / x);)
Example on C # ideone.com/Fanutz.

1 . I do not know how to use a similar mnemonic, so I spied the tablet on the Internet.
2 . "Basics", or "Rule with many exceptions", like most rules.
3 . Initially, in the Roman calendar, February was the last month of the year, so there is logic that it is shorter than everyone else. There is also logic in adding or removing the day at the end of the year, so its length is a variable.

UPD. one:
Alternative translation of the first part in



Did you like the article? Share it