bugtussle
bugtussle

Reputation: 1416

Javascript in Chrome cannot handle date in the format dd/mm/yyyy

Using chrome's js console, I cannot seem to get it to recognize dates formatted in dd/mm/yyyy

> +new Date()
1369840513437
> +new Date("5/28/2013")
1369717200000
> +new Date("28/5/2013")
NaN

The clients computer is in Canada and the regional settings are correct. I've also added the language preference to chrome settings. The above code works as expected in both IE and FF (edit: IE and FF give a number but not the correct number). What an I missing?

Upvotes: 1

Views: 2025

Answers (2)

Yonas Tekeste
Yonas Tekeste

Reputation: 9

   const result = (date: string): string => {
        const [month, day, year] = date.split('/');
        return `${year}/${month}/${day}`;
    };

This is also another solution.

Upvotes: 0

Gabe
Gabe

Reputation: 50493

Well there's not much you can do other than split it up and put it back together, unless you want to use something like moment.js

Bare in mind, this works if you're always expecting the format to be dd/mm/yyyy

var result = "28/5/2013".split("/");
var mydate = new Date(parseInt(result [2], 10),
                  parseInt(result [1], 10) - 1,
                  parseInt(result [0], 10));

Upvotes: 3

Related Questions