Search code examples
javascriptnode.jsdateformatposix

How to convert a Date to POSIX-format in JavaScript (NodeJs)?


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?


Solution

  • 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/