How do I convert a serial number which was initially a date to the number of days that the date initially had?

First I have a date (format of : dd, mm, yyyy) ex : 21, 3, 2012 and I converted it to a serial number which in this case would be 40988. Now for my problem I wanna find an algorithm that returns the number of days given initially so in the example it would be 21. This is the code that I used to convert the date to the serial number :

      //intYears, intMonths, intDays are parameter variables
      var serial = 0

      //365 years in array
      var arrNormalYears = new Array()
      for (let i = 1900; i < intYears; i++) {
         arrNormalYears.push(i)
      }

      //Count the number of normal years excluding the 
      //parameter year (intYear)

      //the function numberOfDaysYear checks for us if the 
      //year is a leap year or a normal year

      var normalYears = 0
      for (let j = 0; j < arrNormalYears.length; j++) {
         if (numberOfDaysYear(arrNormalYears[j]) == 365) {
            normalYears += 1
         }
      }
      //multiply the count and add it to serial

      serial += normalYears * 365


      //Same process for leap years

      var arrLeapYears = new Array()

      for (let m = 1900; m < intYears; m++) {
         arrLeapYears.push(m)
      }

      //Count the number of leap years
      var leapYears = 0
      for (let a = 0; a < arrLeapYears.length; a++) {
         if (numberOfDaysYear(arrLeapYears[a]) == 366) {
            leapYears += 1
         }
      }

      serial += leapYears * 366


      //Now including the parameter variable intYear
      
      //Using the same process as above except this time 
      //its for the months

      var arrMonths = new Array()
      for (let k = 1; k < intMonths; k++) {
         arrMonths.push(k)
      }

      //Here the function numberOfDaysMonth gives us the 
      //number of days for the specific month, it also 
      //checks if it's a leap year 
      //also excluding the last month

      for (let x = 0; x < arrMonths.length; x++) {
         if (numberOfDaysMonth(arrMonths[x]) == 31) {
            serial += 31
         }
         else if (numberOfDaysMonth(arrMonths[x]) == 30) {
            serial += 30
         }
         else if (numberOfDaysMonth(arrMonths[x]) == 28) {
            serial += 28
         }
         else if (numberOfDaysMonth(arrMonths[x]) == 29) {
            serial += 29
         }
      }

      //Simply adding the days that are left
      serial += intDays

      return serial
   }

Now by understanding my algorithm used to turn the date into a serial number, I’m having trouble to in a way reverse the algorithm and return the days as explained above the code.