Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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

      //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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda