convert Java datestring to javascript date

date, javascript, json, string

Solution

You need some JS that parse the String and return the year, month, day, minute,hour, second in strings:

var hour = ds.split(":")[0],
    minute = ds.split(":")[1],
    last_part = ds.split(":")[2],
    second = second_part.split(" ")[0],
    last_part2 = second_part.split(" ")[1],
    day = last_part2.split("/")[0],
    month =  last_part2.split("/")[1],
    year =  last_part2.split("/")[2];

and then instantiate the Date constructor:

var d = new Date ( year, month, day, hour, minute, second );

Problem

When I send a date through JSON from Java to Javascript, it sends something like this: ``` var ds = "11:07:47 13/01/2011"; ``` Javascript fails to parse this string into date ``` var d = new Date(ds); ``` Any ideas?

Original source

Related problems