First you have a serial number which was converted from a date, for example : 40988 would be March 21st 2012. How could I then return 21 knowing that the serial number is 40988 and by NOT using the date object? I also have 2 other functions that I can use : numberOfDaysYear(year) which returns the number of days for a specific year (365 or 366) and I also have numberOfDaysMonth(month, year) which returns the number of days for a specific month and a specific year (31, 30, 28 or 29). The year limit goes from 1900 all the way to 2199, so this is what I started with
`
//Count the number of 365 years and 366 years
var counterNormalYears = 0
var counterLeapYears = 0
for (let i = 1900; i <= 2199; i ) {
if (numberOfDaysYear(i) == 365) {
counterNormalYears = 1
}
else if (numberOfDaysYear(i) == 366) {
counterLeapYears = 1
}
}
` Moving on from here what kind of approach could I take to then find the days that the date has without using the date object?
CodePudding user response:
I'd probably do it like
let day = 40988, year = 1899, month = 0;
while (day >= 0) day -= numberOfDaysYear( year);
day = numberOfDaysYear(year);
while (day >= 0) day -= numberOfDaysMonth( month, year);
day = numberOfDaysMonth(month, year);
To explain ...
- start at 1899, because I use pre-increment for obvious reasons
- first thing that happens is the number of days in 1970 (using YOUR function) is subtracted from days ... since I use pre-increment, 1970 is passed in on the first loop
- since we've gone below zero, add that last number of days in the last used
year
back todays
- similar algorithm for month - subtract from days until below zero
- similar backtrack for month as in step 3
- you may need to adjust
day
by 2 - because of maths