1

This is my mongo db document

      {
        "creatorUid": "1234",
        "creatorUserName": "userabc",
        "title": "this is the first thread",
        "description": "I like this thread and try to read it",
        "threadOwner": "5451e21c7dfe65c6168fe612",
        "createdAt": "2014-10-30T07:04:22.712Z",
        "updatedAt": "2014-10-30T07:04:22.712Z",
        "id": "5451e2f6d61cb61d18ed0122"
      }

I want to format createdAt as 2014-10-30 without time

4

1 回答 1

2

You can format the date in several ways:

  • Easiest is by using moment.js which is a library to manipulate dates in JavaScript. If you need to manipulate dates extensively, then it is worth using the library. Use something like moment('2014-10-30T07:04:22.712Z').format('MM-DD-YYYY') to get the date formatted in any way you want.
  • The second way is to use JS without any library. This can be done as shown below:

var dateObj = new Date('2014-10-30T07:04:22.712Z'); var neededFormat = dateObj.getUTCFullYear() + '-' + dateObj.getUTCMonth() + '-' + dateObj.getUTCDay();

于 2014-11-03T09:26:45.410 回答