开发者

javascript date format not supported - need the format anyway

开发者 https://www.devze.com 2023-03-24 06:59 出处:网络
I need to compare dates but the finnish date format does not seem to be supported (thanks javascript!).

I need to compare dates but the finnish date format does not seem to be supported (thanks javascript!).

Here is the code that should return the difference between 2 dates (one of them being today):

function jämförMedIdag (da开发者_StackOverflow社区tum) {
    if (datum == null || datum == "") {
        alert('Inget datum!');
        return;
    }
    var datum = new Date(datum);
    var dagar = datum.getDate();
    var månader = datum.getMonth();
    var år = datum.getYear();
    var nyttDatum = new Date();
    nyttDatum.setFullYear(år,månader,dagar);
    var idag = new Date();

    if(nyttDatum>idag) {
        var svar = nyttDatum - idag;
        return(svar);
    } else {
        var svar = idag - nyttDatum;
        return(svar);
    }
}

How can I tell javascript that my date is in format dd.mm.yyyy or d.m.yyyy or dd.m.yyyy or d.mm.yyyy?


If you don't want to write a parser from scratch you can give a try to Date.js, an open source library which has 150+ localized plugins. There's a Finnish version too.


There is no function like strptime in JavaScript, so you'll have to parse it yourself by using string manipulation functions like split or match.


Just parse it yourself:

var parts = datum.split('.');
datum = new Date();
datum.setFullYear(parts[2],parts[1]-1,parts[0]);

Etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消