0

I am using PostGreSql which contains date column in one of the table 2021-12-03T00:00:00.000Z. I developed API using nodejs to get the data from tables.

Below is the function I wrote to format the date and pass to the class. This works perfectly fine and I recieve 2021-12-03 as output

    formatDate(date) {
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + (d.getDate() + 1),
            year = d.getFullYear();
    
        if (month.length < 2) 
            month = '0' + month;
        if (day.length < 2) 
            day = '0' + day;
    
        return [year, month, day].join('-');
    }

    async getData(date){

        return {
            DataLine: await this._dateLine.getData1(d)
        }
    }

app.js

class Data extends Model {

    constructor(conn, schema) {
        super(conn, schema, "data", {
           
            Date: date(true),
            number: integer(true),  
          ...many other column
        }, true);
    }

    async getData1(date) {
        return (await this._permanent.findAll({ where: { td:date } })).map((term) => ({
            td: term.transDate,
            number: term.number,
        }));
    }
}

Is there a way to require them in the "yyyy-mm-dd" format to begin with and not parsing /reformatting as I did above as I am already getting date in format yyyy-mm-dd, my function redo that ?

Dharmisha Doshi
  • 137
  • 3
  • 9
  • Why do you need to parse and reformat? – evolutionxbox Feb 01 '22 at 00:42
  • @evolutionxbox because this is the original date when I console.log - `2021-12-03T00:00:00.000Z` – Dharmisha Doshi Feb 01 '22 at 00:47
  • I dont think the above link answers my question – Dharmisha Doshi Feb 01 '22 at 00:48
  • What about adapting [this](https://stackoverflow.com/questions/6177975/how-to-validate-date-with-format-mm-dd-yyyy-in-javascript) instead? – evolutionxbox Feb 01 '22 at 00:54
  • I think you got my question wrong, I am not trying to validate date. Instead looking for ```"yyyy-mm-dd" format``` that I can require to avoid writing above `formatDate` function – Dharmisha Doshi Feb 01 '22 at 00:59
  • If the starting value is "2021-12-03T00:00:00.000" and you want just the date part as "2021-12-03" then just split on the "T" or use `"...".substring(0,10)`. That will keep the UTC date. If you want the equivalent local date, then `new Date(timestamp).toLocaleDateString('en-CA')` does the job. If that's the case, then this is a duplicate of [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|2041.5433). – RobG Feb 01 '22 at 03:47
  • Hi, I do not need today's date. I am getting date value from database @RobG – Dharmisha Doshi Feb 01 '22 at 21:54
  • So, is your question here really about your database? Are you asking how to get your database to give you dates in the form of `yyyy-mm-dd` instead of `2021-12-03T00:00:00.000Z`? I don't know your database, but `yourDate.replace(/T.*$/, "")` will convert `"2021-12-03T00:00:00.000Z"` to `"2021-12-03"` in one string manipulation step. – jfriend00 Feb 02 '22 at 00:26
  • I get date in format `2021-12-03T00:00:00.000Z` from database. What I want to display to UI is `2021-12-03`. In the above function `formatDate` I am getting month, day, year and then joining with `-`. (parsing and reformatting which is not needed) However I am already getting date with `-`. In this case above function is not optimal. So I was looking for solution where I do not have to repeat logic of joining date with `-`,. I am getting `"routine": "DateTimeParseError",` when use `.replace(/T.*$/, "")`. Date is of type `date` in database. Hope its clear – Dharmisha Doshi Feb 02 '22 at 00:43
  • I guess I still don't quite understand what you're trying to do, but if you have a Javascript `Date` object from your database and you want to convert it to a string like this `"2021-12-03"`, you can do `yourDate.toJSON().replace(/T.*$/, "")`. That result will be a string for display which is what I thought you were asking for. You cannot directly give that back to your database as a Date without first converting it back to a Date object. – jfriend00 Feb 02 '22 at 01:04
  • FYI, all the other answers where you responded "I do not need today's date", you can just substitute your own date object instead of where they show `new Date()`. They are just using that for illustration purposes to get some date they can show you how it works. – jfriend00 Feb 02 '22 at 01:08
  • Got it, thanks. I will try `yourDate.toJSON().replace(/T.*$/, "")` – Dharmisha Doshi Feb 02 '22 at 02:24
  • Thanks for sticking by, I am still learning. Should this work `let d= date.toJSON().replace(/T.*$/, "") console.log(d)` – Dharmisha Doshi Feb 02 '22 at 02:31
  • 1
    The reason there are so many comments is that it's extremely difficult to work out what you want. [Nikita's answer from yesterday](https://stackoverflow.com/a/70934531/257182) will do what you want and is effectively identical to what you're proposing now. `date.toJSON` gives exactly the same result as `date.toISOString`. That will give you the UTC date. My (deleted) answer returns the local date. – RobG Feb 02 '22 at 04:06
  • @RobG @jfriend00, I used Nikita code and here is the output I am getting. I do not know why it gives `Invalid Date`. Code - `formatDate(date) { console.log('date', date) const d = new Date(date) console.log('New Date', d);}` and Output - `date 03122021 New Date Invalid Date` If I use `date.parse()` or `d.toISOString()` I do not get `console.log output` – Dharmisha Doshi Feb 02 '22 at 17:58

1 Answers1

0

One of the easiest ways to get yyyy-mm-dd is new Date().toISOString().split('T')[0]

Nikita
  • 121
  • 6