Lua language, two-year difference in days __lua language

Source: Internet
Author: User
Tags lua

Ask to calculate how many days the date is from October 1, 1949

For example:

User entered: 1949-10-2

Program output: 1

User entered: 1949-11-1

Program output: 31

In total, two classes were written, and then the code was slightly scrambled:

Yearandmonth:

Require ("Yearprint")

Local nyear = 1949
Local Nmonth = 10
Local Nday =1

--[[--
--@param #number year passed the argument is the number of years
--@param #number month passed the parameter is the month
--@param #number Day passed the parameter is the date
--@return #day return the number of days of the year
--]]

Local function getdays (year,month,day)
Local Temp ={31,28,31,30,31,30,31,31,30,31,30,31}
--Judge whether it is a leap year, if for leap year, change February is 29, otherwise change February is 28
if ((year%4==0 and year%100 ~= 0) or year%400==0) then
TEMP[2] = 29
Else
TEMP[2] = 28
End
For i = 1,month-1 do
Day = day + temp[i];
End
--Returns the number of days from this year to the month
Return day;
End

--[[--
--the number of years passed by @param #number year
--@return #number determine whether the number of years is a leap year, if it is a leap, return 366 days, vice versa return 365 days
--]]
Local function days (year)
--judge whether leap year, if leap year, return 366 days, if not leap year, return 365 days
Return ((Year%4==0 and year%100 ~=0) or year%400==0) and 366 or 365;
End

--[[--
--@param the number of days #number sum of two years
--@param #number year entered
--@param #number the month entered by month
--the date entered @param #number day
--@param #number initial year of Nyear
--@param #number initial month of Nmonth
--@param #number initial date of Nday
--@return #number sum is the number of days difference between two years
--]]
Local function Sumday (year,month,day)
Local sum = 0;
--Less than nyear years
If year < nyear then
For x = year,nyear-1 do
--Passing years past, number type
sum = sum + days (x);
End
--The number type is passed
sum = sum + getdays (nyear,nmonth,nday);
sum = Sum-getdays (year,month,day);
ElseIf year = = Nyear Then
--if it's the same year, judge the size of the month, and then go to absolute
sum = Math.Abs (GetDays (year,month,day)-getdays (nyear,nmonth,nday));
Else
--More than nyear years
For i = Nyear + 1, year do
sum = sum + days (i);
End
sum = sum + getdays (year,month,day);
sum = Sum-getdays (nyear,nmonth,nday);

End
Print ("Days of Difference:". SUM);
return sum
End

--[[--
--@param #class yearprint Ymdprint is a file encryption, Printyearmonthday () Enter the month and day method
--@param An array of data localized #table, and accepts the Yearprint
--@param the number of days #number sum of two years
--@param #number year entered
--@param #number the month entered by month
--the date entered @param #number day
--]]

Local function Isleapyear ()
Local data = {}
data = Ymdprint.printyearmonthday ()

Local year = Tonumber (data. year);
Local month = Tonumber (data. Month);
Local day = Tonumber (data. Day);
Sumday (Year,month,day)
End

--[[--
--@isLeapYear is the way to calculate the number of years in the month
--]]
function Main ()
Isleapyear ()
End

Main ()
The second class Yearprint:

Module ("Ymdprint", Package.seeall);

--Isflag is initialized to False, determines whether the year is a leap years, or true if it is a leap, or false
Local Isflag = False
--[[--
--an array of @param #table ADTA days
--@param #number data. Year entered in
--@param #number data. Month the Month entered
--@param #number data. Date entered by day
--@return #table data Returns an array that holds the date of the input.


--]]
function Printyearmonthday ()
Local data = {};
Print ("Enter the year (0-9999) and press ENTER:")
Data. Year = Io.read ()
If Tonumber (data. Year) ~= nil) Then
If Tonumber (data. Year) > 0 and Tonumber (data. Year) < 9999) Then
if (Tonumber (data. Year)%4==0 and Tonumber (data. Year)%100 ~=0) or Tonumber (data. Year)%400==0) then
Isflag = True
End
Print ("Please enter the month and press ENTER:")
Data. Month = Io.read ()
Else
Print ("The year entered is 1-9999, please re-enter it.") ")
Printyearmonthday ()
End
Else
Print ("Malformed, please re-enter.") ")
Printyearmonthday ()
End
If Tonumber (data. Month) ~= Nil) Then
If Tonumber (data. Month) > 0 and Tonumber (data. Month) <) Then
Print ("Please enter a date and press ENTER:")
Data. Day = Io.read ()
Else
Print ("The month entered is January-December, please re-enter it.") ")
End
If Tonumber (data. Day) > 0 and Tonumber (data. Day) then <)
if (Isflag = = True and Tonumber (data). Month) = = 2) Then
If Tonumber (data. Day) = Then
Print (the date is entered correctly.) ")
Else
Print ("Is a leap year, should be 29 days, input error, please re-enter");
Printyearmonthday ();
End
End


if (not Isflag and tonumber, data. Month) = = 2) Then
If Tonumber (data. Day) then <=)
Print (the date is entered correctly.) \ n ")
Else
Print ("Not leap year February, should be 28 days, cannot be greater than, input error, please re-enter:")
Printyearmonthday ();
End
End
Else
Print ("The date entered should be between 1-31.") ")
Printyearmonthday ();
End
Else
Print ("Malformed, please re-enter.") ")
Printyearmonthday ()
End
return data;
End

If there is a mistake, welcome everyone to discuss together

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.