21

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 2012-05-16 14:59:18. But when I retrieve the same data by Node.JS, the data is displayed in the browser as

Fri Jun 08 2012 15:16:50 GMT+0530 (India Standard Time)

Why this format change is happening?

I have written this query:

SELECT
biz_registration.reqid,biz_registration.request_date,biz_registration.req_status,biz_registration.annual_cost,biz_registration.rid,biz_contact.first_name,biz_contact.last_name
FROM biz_registration
INNER JOIN biz_contact ON biz_registration.reqid=biz_contact.reqid
ORDER BY biz_registration.request_date
DESC limit '+start+','+mlimit+''

the html code i am using is,

options +='<div class="column" style="background-color: #E1E1E1;width:         100px;">'+result.reqid+'</div>';
options +='<div class="column" style="background-color: #E1E1E1;width: 160px;">'+result.request_date+'</div>';
nithin Kumar
  • 295
  • 1
  • 2
  • 8
  • How are you converting the date from the query into a `Date` object in Node.JS? How are you displaying the date in HTML? The query is probably fine, this is HTML formatting or `Date` object creation issue. – freakish Jun 25 '12 at 11:01
  • Please post the code that you use to convert the date into a js Date object (if you do) and the code that generates the html. In js Date-objects the months start at 0. January is month 0, June is is month 5. – kioopi Jun 25 '12 at 11:04
  • What library are you using for MySQL - Node.JS connection? – freakish Jun 25 '12 at 11:16
  • i am not doing any date object conversion.can anyone help me in doing it.thanks in advance – nithin Kumar Jun 25 '12 at 11:32
  • thank u to all who gave the hints. – nithin Kumar Jun 25 '12 at 12:12

6 Answers6

34

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719
Balkana
  • 686
  • 6
  • 5
  • Could you please explain more your solution? – abarisone Apr 05 '15 at 13:49
  • I had a customer database with some orders and when I wanted to display Date of the order I would get a very long date format (example from first post) Then I was trying to find a simple solution so I can just display the actual date 'MM/dd/YYYY' and with the code from above it worked perfectly. I found that example on some other site, can't remember right now. This is how it looks at me right now: var pool = mysql.createPool({ connectionLimit : 10, host : 'localhost', user : 'root', password : 'password', database : 'database', dateStrings: 'date' }); – Balkana Apr 05 '15 at 21:10
  • 2
    All the options from the node-mysql docs: dateStrings: Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date objects. (Default: false) -- I was looking for DATETIME myself – user353885 Oct 05 '15 at 04:24
21

This config resolve mine, according to node-mysql doc

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

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.

nithin Kumar
  • 295
  • 1
  • 2
  • 8
5

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" );
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
2

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

Martin Faartoft
  • 3,275
  • 2
  • 18
  • 11
0

Sometimes you can't get to format your date with the yyyy-mm-dd h:MM:ss (database format) just by using new Date().toLocaleString or new Date().toISOString().

To achieve this we need to add the dateformat package

npm install dateformat

Then we use

const dateFormat = require ('dateformat');

So to assign a date you can use dateFormat

var created_at = dateFormat(new Date(), 'yyyy-mm-dd h:MM:ss');

Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47