Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to convert a given date from dd.mm.yyyy to yyyy-mm-dd. I tried the Code below. But if i'm giving a "22.10.2018" the script Returns "2018-11-21T23:00:00.000Z". But why?

What I have tried:

function hasError() {
    function toDate(datestr) {
        var parts = datestr.split(".")
        return new Date(parts[2], parts[1], parts[0])
    }

    var comparestart = toDate("22.10.2018");
    var compareende = toDate("02.11.2018");   

    console.log(comparestart);
    console.log(compareende);

    if(comparestart >= compareende) {
        console.log(true);
    } else {
        console.log(false);
    }
}

hasError();
Posted
Updated 19-Oct-18 10:58am
Comments
F-ES Sitecore 19-Oct-18 8:17am    
comparestart etc are dates and you're asking a date to represent itself as a string, so it shows itself in its default format. If you want your date in a specific format then you'll have to write a function that outputs the various date parts in the desired format, or use a library that has these functions already.

If you google "javascript format date" you'll find many many examples of how to format dates.
Richard MacCutchan 19-Oct-18 9:08am    
Probably because the date has been adjusted to the locale's timezone.
Richard MacCutchan 19-Oct-18 9:32am    
Of course; thank you for reminding me.

There are 2 issues with the code:
1 - "Date" object asumes month number to start in 0, not 1. The line:
new Date(parts[2], parts[1], parts[0])
should be:
new Date(parts[2], parts[1] - 1, parts[0])

2 - Notice "new Date" asumes hour to be in your local timezone (also affected by daylight saving). In this case, new Date(year, month-1, day) creates a date in your local 00:00:00 time. Which of course would be a different local hour for any other timezone. Any location on the west to you would be the previous date.

To avoid timezone issues, use always UTC functions. Eg:
new Date(Date.UTC(parts[2], parts[1] - 1, parts[0]))
 
Share this answer
 
v2
Hello, thank you very much for all your help. Finally i used that Code:

function hasError() {
    function toDate(datestr) {
        var parts = datestr.split(".")
        return new Date(parts[2], parts[1] -1, parts[0]).toLocaleString('de-DE', {timeZone: 'Europe/Berlin'})
    }

    var comparestart = toDate("$Start");
    var compareende = toDate("$Ende");

    if(comparestart >= compareende) {
        return true;
    } else {
        return false;
    }
}


Because it will just executed by our german customers, that could be acceptable.

Thank you all again.
 
Share this answer
 
Warning: Solution 2 can result in a wrong date. The code: new Date(...) still uses your local machine timezone.

For instance, if you run this code in California you'd get: 2018-10-19 00:00:00 GMT-8 -> 2018-10-19 10:00:00 in Germany (same day).

But if you run this code in Beijing, China, you'd get: 2018-10-19 00:00:00 GMT+8 -> 2018-10-18 18:00:00 in Germany. The previous day!

If you want to get rid of timezone issues, I'd recommend you to always use UTC. For instance:
function toDate(datestr) {
    var parts = datestr.split(".");
    return new Date(Date.UTC(parts[2], parts[1] -1, parts[0]));
}


var comparestart = toDate("$Start");
var compareende = toDate("$Ende");

if (comparestart >= compareende) {
    return true;
} else {
    return false;
}
To display the date you can use any of the following:
date.toUTCString()
date.toLocaleString('de-DE', {timeZone: 'UTC'})
date.getUTCDate() + '.' + (date.getUTCMonth() + 1) + '.' + date.getUTCFullYear()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900