0

I need to convert a date to posix-format in order to use an API (WebSocket)

my Request: { "InstrumentId": 1, "FromDate": // POSIX-format date and time }

I'm not familiarized with Date manipulation. How can I get today's date and convert to posix-format?

2 Answers2

-1

There is awesome library for date manipulations called moment.js. Just install it using

npm install --save moment

Code

my_Request: {
    "InstrumentId": 1,
    "FromDate": moment().format("MM/DD/YYYY H:M:S") // POSIX-format date and time
}

You can follow there documentation for more formats https://momentjs.com/docs/

Mr. Ratnadeep
  • 591
  • 4
  • 10
  • The default format for POSIX dates is `%Y-%m-%d`, e.g. 2018-03-02. A good answer should not rely on a library that isn't tagged or mentioned in the OP. Creating a POSIX format date is equivalent to creating an ISO 8601 format date, and there are already [many, many questions and answers](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+format+a+date) for that. – RobG Mar 03 '19 at 08:50
-1

You can use a library such as moment.js to do this easily. This allows you to convert a Javascript date object into any format you please

var date = new Date()
moment(date).format("MM/DD/YYYY H:mm:ss")
spazhead
  • 304
  • 3
  • 14