MARKAND Bhatt
MARKAND Bhatt

Reputation: 2640

Danish date to new DATE()

ons okt 01 2014 10:00 AM

This is wed oct 01 2014 10:00 AM in danish language.

var dt = new Date("ons okt 01 2014 10:00 AM");

the above line gives me "invalid date".

What to do to get proper date in dt variable?

Upvotes: 0

Views: 1394

Answers (2)

Mottie
Mottie

Reputation: 86453

I would recommend using a date library:

Upvotes: 1

Claudi
Claudi

Reputation: 5416

Since the format itself is valid for Date to correctly parse the string, you should implement a function that translates the day of week and month from Danish to English.

function fromDanish(dateString) {
    //A table whose properties are Danish names for days of week and whose values are English names.
    var daysOfWeek = {'son': 'sun', 'man': 'mon', 'tir': 'tue', 'ons': 'wed', 'tor': 'thu', 'fre': 'fri', 'lor': 'sat'};

    //A table whose properties are Danish names of months and whose values are English names.
    var months = {'jan': 'jan', 'feb': 'feb', 'mar': 'mar', 'apr': 'apr', 'maj': 'may', 'jun': 'jun', 'jul': 'jul', 'aug': 'aug', 'sep': 'sep', 'okt': 'oct', 'nov': 'nov', 'dec': 'dec'};


    var tokens = dateString.split(' '); //Split the Danish string by spaces
    var dow = tokens[0]; //This is the Danish day of week
    var month = tokens[1]; // and this is the Danish month 

    tokens[0] = daysOfWeek[dow]; //Get the English day of week from the table
    tokens[1] = months[month]; //Get the English month from the table
    var english_date = tokens.join(' '); //Join all tokens again
    return new Date(english_date); //Now Date() constructor knows how to parse the date string
}

Upvotes: 2

Related Questions