0

I'm trying to convert text from a div on a website to a date variable, to be able to sort the divs on date descending.

However I'm having issues with the month october: enter image description here

datetest = new Date("10 oktober 2020");

What am I doing wrong?

All other months work, except oktober.

Elvira
  • 1,410
  • 5
  • 23
  • 50

3 Answers3

3

Using moment

// Change the locale globally
moment.locale("nl");

console.log(moment("10 oktober 2020", "DD MMM YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment-with-locales.min.js" integrity="sha512-qSnlnyh7EcD3vTqRoSP4LYsy2yVuqqmnkM9tW4dWo6xvAoxuVXyM36qZK54fyCmHoY1iKi9FJAUZrlPqmGNXFw==" crossorigin="anonymous"></script>
User863
  • 19,346
  • 2
  • 17
  • 41
2

So the months in Dutch have the first 3 chars in common with English except

mei and oktober which will fail.

You can use a lookup table:

const months = {
  "jan": "Jan",
  "feb": "Feb",
  "mar": "Mar",
  "apr": "Apr",
  "mei": "May",
  "jun": "Jun",
  "jul": "Jul",
  "aug": "Aug",
  "sep": "Sep",
  "okt": "Oct",
  "nov": "Nov",
  "dec": "Dec"
}

console.log(new Date(`1 ${months["mei"]} 2020`))

console.log(new Date(`1 ${months["oktober".slice(0,3)]} 2020`))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • The answer below using moment or luxon is more generally applicable (what if you wanted whole month names, what if you had another language/locale). –  Jul 08 '20 at 12:19
  • @mrblewog Sure. But not applicable if not. You can add a substring if you want - see update – mplungjan Jul 08 '20 at 12:20
0

You have to provide the month string in english. It can be abbreviated to 3 characters. E.g. "October" -> "Oct" will work, which is probably why some of the months in dutch worked if they share the first 3 characters with english months.

triihim
  • 62
  • 4