Home > Software design >  How do I convert a serial number which was initially a date to the number of days that the date init
How do I convert a serial number which was initially a date to the number of days that the date init

Time:04-15

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

      //Going from 1900 to intYears - 1 (excluding the last year)
      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.

CodePudding user response:

You should use setDate and replace the number of days instead of adding it as others have said. The JS Date constructor allows number that are larger than you would normally think were allowed and gives the correct date.

let serial = 40988;
let dayOfMonth = new Date(1900, 0, serial).getDate();
console.log(dayOfMonth);

UPDATE

If you set the serial as zero you would get 31 December 1899 - You have managed to recreate OADate! Didn't even notice that originally

CAUTION

JavaScript deals with dates in your local timezone. So long as you stick to a single timezone (local/UTC/whatever) you should be okay.

  • Related