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

96
Visualizações
Troubleshooting a Date That Won't Parse in Javascript

Getting date properties back from a C# web API that seemed fine but ran into issues when plugging it into DevExtreme DateBox. It was throwing an error of 'getFullYear is not a function' so I checked the dates against this function I found here -

  let r: any = http.post('/get', { Param1: 2, Param2: 1 });
  console.log(r.StartDate);  
  console.log(this.isValidDate(r.StartDate));
  r.StartDate = new Date(r.StartDate);
  r.EndDate = moment(r.EndDate);
  console.log('Start Date', this.isValidDate(r.StartDate));
  console.log('End Date', this.isValidDate(r.EndDate));

    isValidDate(d: any): void {
        if (Object.prototype.toString.call(d) === "[object Date]") {
            console.log('it is a date');
            if (isNaN(d)) { // d.getTime() or d.valueOf() will also work
                console.log('date object is not valid');
            } else {
                console.log('date object is valid');
            }
        } else {
            console.log('not a date object');
        }
    }

  StartDate: "/Date(1657512000000)/"

  not a date object
  undefined

  it is a date
  date object is not valid

  Start Date undefined
  not a date object

  End Date undefined

Not sure why this hasn't come up before with this API but didn't want to look to DevExpress given that I can't produce a valid date.

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

0

I'm providing this answer to demonstrate one way to parse out the timestamp in the string you have of the following format, inferred by console.log(r.StartDate); ... /Date(TS)/:

// Provided the date has the following structure in a string

var anyStartDate = "/Date(1657512000000)/";

// Prepare to parse it out by getting the positions of the parentheses

var openParens = anyStartDate.indexOf("(");
var closeParens = anyStartDate.indexOf(")");

// Parse out the timestamp

var timeStampStr = anyStartDate.substring(openParens + 1, closeParens); 

console.log( timeStampStr ); // 1657512000000

// Convert timestamp to an int. You can do this when you create the obj, but I am separating it here for explanation purposes.

var timeStampInt = parseInt( timeStampStr );

// Now create a date object

var dateObj = new Date( timeStampInt );

console.log( dateObj );

// (on the machine I'm on):
// Outputs: Mon Jul 11 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
// Or outputs: 2022-07-11T04:00:00.000Z

Now I don't know which library(ies) you are using to handle dates so I just went with the native Date object. You can use this SOLUTION however on further insights to apply it to your code.

The point is once the timestamp is extracted, it can be then used to create a Date object, and thus utilize all the methods that are inherent to that class.

In terms of the "timezone", to get it to UTC, it's already in UTC but javascript formats it to your computer's locale. Internally it's still UTC. There's a way to display it as strictly UTC which is in the docs.

`

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