Dario Calovic Dario Calovic - 4 months ago 51
Javascript Question

Given string format to date

I have given a very unusual date format in

string
. I need to convert it to a JS date object and add up a few days. The last step is clear, but I don't know how to convert the string into the JS date object. Take a look at the string date:
October 02, 2016

Answer

You should use moment.js

Syntax:

moment(dateString, format, locale)

var dateStr = "Oktober 02, 2016"
var d = moment(dateStr, "MMM DD, YYYY", 'de');
console.log(d.format("DD-MM-YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment-with-locales.min.js"></script>