skip to Main Content

created a simple crud application on nodejs and mysql. but have one problem, cant display the date without time and in the proper format.

{
"id": 1,
"name": "Rick Sanchez",
"dob": "1960-05-20T19:00:00.000Z",
"phone": "84351548898",
"comments": "Genius in the galaxy"
},

in the phpmyadmin, the date in the dob i chose as the datetime format.

3

Answers


  1. You can use toLocaleDateString() method,

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

    const obj = {
     "id": 1,
     "name": "Rick Sanchez",
     "dob": "1960-05-20T19:00:00.000Z",
     "phone": "84351548898",
     "comments": "Genius in the galaxy"
    };
    
    const dateOfBirth = (new Date(obj.dob)).toLocaleDateString();
    
    console.log(dateOfBirth);
    Login or Signup to reply.
  2. You could create a date Object from the string with const myDate = new Date(myString) and then use a method like myDate.toLocaleDateString() to retrieve only the date part

    Login or Signup to reply.
  3. I believe you should use the moment.js library which is more powerful and has great options(like format).

    In your case, you can do the following.

    let date = moment('1960-05-20T19:00:00.000Z').utc().format('YYYY-MM-DD');
    console.log(date);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search