-1

I am encountering a really strange error when created a new data in javascript from the string. My String looks like this 2015-12-04T01:42:13 The code is the following

date = new Date(2015-12-04T01:42:13)
console.log(date);

The result of the console.log is 2015-12-04T02:42:13 but why? I now that UTC is +1 in my timezone. But I am taking the Value out of the database and comparing it with the actual date to see how much time has passed. I do not want it to be corrected as it hase been obtained in this timezone already. Why is this so? What is the logic? How can i create the exact Date from a String?

This actually seems to be a Problem with node.js. I cannot reprodouce it everywhere.

Silve2611
  • 2,198
  • 2
  • 34
  • 55
  • My guess would be that the ISO 8601 format assumes UTC if there is no offset supplied – Phil Dec 04 '15 at 01:09
  • it is not a duplicate. My input is a datestring obtained from the database. – Silve2611 Dec 04 '15 at 01:15
  • So what timezone should the date string be in? JavaScript is interpreting your string as UTC so if you just want to compare it to another date, use UTC for that one too – Phil Dec 04 '15 at 01:21
  • The Problem is differen. The Value is obtained at 13:00 Already with UTC +1. Then it is written into the SQLITEDB as a String. At 13:30 i take it out of the db and want to see how much time has passed. If it adds 1 Hour to the date i get -30 insted of +30.... – Silve2611 Dec 04 '15 at 01:23
  • 1
    "Already with UTC +1" --- how would DB and JS know that? If it is a `+01:00` timezone there - it should be represented. – zerkms Dec 04 '15 at 01:24
  • I obtain a date at a specific date. I have a batch script that checks how much times has passed so i need the date from the db in the js date format again – Silve2611 Dec 04 '15 at 01:25
  • 1
    Your current string specifies time in `UTC`, so it's interpreted as such. If you want it to be interpreted with a particular timezone taken into account - put it to that datetime string explicitly. – zerkms Dec 04 '15 at 01:26
  • Is it not possible to create a javascript Date object from a string that is not modified, bet exactly represent the sting? – Silve2611 Dec 04 '15 at 01:29
  • It is possible and you have created it in the code in your question. – zerkms Dec 04 '15 at 01:29
  • No because if i log it afterwards it adds 1 hour – Silve2611 Dec 04 '15 at 01:31
  • It does create an object exactly as you requested. It's not a JS problem that **your data** is incorrect. – zerkms Dec 04 '15 at 01:33

1 Answers1

-1

Thanks to @zerkms i found a way to get the Data right. I Wrote 2 function to ensure the data is saved the right way in the database. It delivers me the string in the following format YYYY-MM-DDTHH:MM:SS+GMT(2015-12-05T13:11:59+01:00). This string can be interpreted correctly with 'new Date("2015-12-05T13:11:59+01:00")'

var helper = new function(){
    //getting the timezone in correct format
    this.getTimeZone = function(){
        var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
        return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}

    this.dateString = function(date){
        var year = date.getFullYear();
        month = date.getMonth() + 1; // months are zero indexed
        month = ("0" + month).slice(-2);
        day = date.getDate();
        day = ("0" + day).slice(-2);
        hour = date.getHours();
        hour = ("0" + hour).slice(-2);
        minute = date.getMinutes();
        minute = ("0" + minute).slice(-2);
        second = date.getSeconds();
        second = ("0" + second).slice(-2);
        timezone = helper.getTimeZone();

        return year + "-" + month + "-" + day + "T" + hour + ":" +
        minute + ":" + second + timezone;
    }
}
Silve2611
  • 2,198
  • 2
  • 34
  • 55
  • It will break all the previous dates when the machine switches to a summer/winter time. So it's not "the answer", it's a workaround that at this moment accidentally happens to work. – zerkms Dec 04 '15 at 01:40
  • Good point thank you. But still how else can i compare a date taken one hour before and saved to the database with the actual one? – Silve2611 Dec 04 '15 at 01:47
  • Actually i do not share your opinion. Tha data is written to the db without the GMT. That is the reason it is misinterpreted with new date. My solution fixes it – Silve2611 Dec 04 '15 at 02:13
  • "Tha data is written to the db without the GMT." --- that's correct. The actual timezone is lost. Which means if your local timezone changes due to winter/summer time - the older dates will be either correct or shifted by 1hr. Which means the data will be incorrect. "My solution fixes it" --- it does not. – zerkms Dec 04 '15 at 02:30
  • "But still how else can i compare a date taken one hour before and saved to the database with the actual one?" --- you **MUST** save it in the format that carries timezone. If the timezone data is lost - you cannot* recover it reliably. – zerkms Dec 04 '15 at 02:30
  • Thank you for your input. And I see that you are wright. But the timeframes that are beeing compared are to short to make GMT necessary. The Problem is that i do not want to change the specified format. How do i add a gmt to thsi format above? I could not find andything – Silve2611 Dec 04 '15 at 10:53
  • You cannot, since you don't know what timezone to add - since it was lost. – zerkms Dec 04 '15 at 20:27
  • I now that it is los but i could add it next time when i save it to the db. How could i enhance the existing string with a gmt or do i have to use a different format? – Silve2611 Dec 06 '15 at 22:50
  • You have to use a format that carries a timezone https://en.wikipedia.org/wiki/ISO_8601 – zerkms Dec 06 '15 at 22:53