date format in node.JS

前端 未结 5 1939
一个人的身影
一个人的身影 2021-02-01 21:22

I am using mysql database,in that i have a field by name request_date. The type of the field is time stamp and the data stored in this field has the format 20

相关标签:
5条回答
  • 2021-02-01 21:23

    I was having the same problem, this fixed my problem:

    You need to 'force' your mysql connection to format it immediately, add this into your config(connection details):

    host: 'localhost',
    user: 'root'
    // ....
    dateStrings: 'date'
    

    Note that in many cases

    dateStrings: true
    

    rather than 'date'

    seems to be the needed solution.

    0 讨论(0)
  • 2021-02-01 21:31

    What you see, is simply the default javascript date formatting. If you want it formatted differently, you can use the methods:

    getDate(): Returns the date
    getMonth(): Returns the month
    getFullYear(): Returns the year
    

    on the date object, and combine the results with / or - or : to get the format you want.

    Have a look at: http://www.elated.com/articles/working-with-dates/ for more details

    0 讨论(0)
  • 2021-02-01 21:35

    i got this working by requiring the library or module called date format. first we have to install date format package using

    npm install dateformat

    then u can require it in ur coding. then u can create the object of retrieved data as

    var day=dateFormat(result.request_date, "yyyy-mm-dd h:MM:ss");

    and print it.

    0 讨论(0)
  • 2021-02-01 21:40

    This config resolve mine, according to node-mysql doc

      host: 'localhost',
      user: 'root',
      password: '',
      dateStrings:true,
      database: "mydb"

    0 讨论(0)
  • 2021-02-01 21:41

    Here is the solution :

    var datetme = new Date().toLocaleString();
    

    OR

    const DATE_FORMATER = require( 'dateformat' );
    var datetme = DATE_FORMATER( new Date(), "yyyy-mm-dd HH:MM:ss" );
    
    0 讨论(0)
提交回复
热议问题