iam
iam

Reputation: 1013

Timestamp in node js different from database

In database time-stamp is saved as 2015-12-05 10:53:12 but when I fetch record using mysql query it return this time-stamp like 2015-12-05T10:53:12.000Z. Why? any issue?? please help.

Upvotes: 0

Views: 1064

Answers (3)

Matt Searles
Matt Searles

Reputation: 2765

You can use MySQL's DATE_FORMAT, eg.

SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); -> 'Sunday October 2009'

http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

Upvotes: 0

nicost
nicost

Reputation: 1040

To convert your Date-Object (which is represented as ISOString with "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") you have to parse it after reading from the db.

In JavaScript you can do that by simply passing the string in the constructor, e.g. var myDate = new Date("2015-12-05T10:53:12.000Z")

With a custom format you could do it like this for example:

var dateString = "2015-12-05 10:53:12"; var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");

In order to represent it as "Dec 16, 2015" you have to parse it afterwards:

There are some good libraries for doing this (e.g. momentjs or with angularjs - date filters)

Without that you have do manually do it somehow like this (where you pass your date object created before):

  function parseDate(date) {
    var months = ['JAN','FEB','MAR','APR','MAY','JUN',
      'JUL','AUG','SEP','OCT','NOV','DEC'];
    return months[date.getMonth()]+" "+("0" + date.getDate()).slice(-2)+", "+date.getUTCFullYear();
  }

Upvotes: 1

Amit
Amit

Reputation: 46323

In your database the timestamp is saved in binary numerical format. "2015-12-05 10:53:12" & "2015-12-05T10:53:12.000Z" are string representation of that value that you see in some application.

It's the same value and there's no issue to solve here.

Upvotes: 2

Related Questions