peter flanagan peter flanagan -3 years ago 97
Javascript Question

change date format javascript

I need to send a date to a backend service that requires a date in the following format.

I have access to moment also.

I am using an input type of

datetime
on the front end which sends over a date like this:
"2017-05-17T10:00"


I have tried
new Date("2017-05-17T10:00");
but this returns
Wed May 17 2017 11:00:00 GMT+0100 (BST)
. I have also tried using some
moment
methods, but cannot get the correct format.

Does anyone know how I can convert the datetime string -
"2017-05-17T11:43"
to the following
'2017-05-17T10:43:03+0100'
?

Answer Source

Try moment.format(). Here is the list for reference https://momentjs.com/docs/#/displaying/format/

var dt = new Date("2017-05-17T10:00");
console.log(dt);

//'2017-05-17T10:43:03+0100'

var z = moment(dt).format("YYYY-MM-DDTHH:mm:ssZZ");
console.log(z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download