convert Data typescript

Asked

Viewed 213 times

0

I’m having a relatively simple problem, but I’m not finding a solution:

I need to send a request to save a Date in the database(sqlServer), but the date has to be in the format (yyyy-dd-mm HH:mm:ss). My object however, passes as follows (2010-4-4), soon, I need him to turn 2010-04-04 00:00:00

I used the new Date(p) passing this date as parameter, but it does not recognize this format, and at the end returns me NaN

how can I solve this problem? follow my current code

    public formatData(d) {

    let date    = new Date(d);
    console.log(date)

    var ano  = date.getFullYear();
    var mes  = '' + (date.getMonth() + 1);
    var dia  = '' + date.getDate();

    if (mes.length  < 2) mes  = '0' + mes;
    if (dia.length  < 2) dia  = '0' + dia;

    return [ano, dia, mes].join('-') + ' ' + ['00', '00', '00'].join(':');
}
  • 1

    I do not understand what you want to do, if you pass the date 2010-4-4 by that your function does not return Nan, nor if I run a new Date() with the return of it, the error is in another part of the code that is not here?

  • 1

    Why the Typescript tag? It wouldn’t be the same if you used Javascript normally?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.