-1

I thought I had a standard ISO date and the Javascript Date() function should convert it with no parsing, but I get the following:

my date :

2015-12-08T13:42

Javascript date function result:

Tue Nov 12 2075 13:42:00 GMT+0100

Is my date in an incorrect format or have I misunderstood the Data function?

I should have added that I'm using Adobe Illustrator CS6. I've tried an examples from below and I get the same result:

My code:
var start = '2015-12-08T13:42';
var test0 = new Date(start);
var test1 = new Date(start+'Z').toString();
$.writeln(test0);
$.writeln(test1);

The result:

Tue Nov 12 2075 13:42:00 GMT+0100
Tue Nov 12 2075 13:42:00 GMT+0100
user5072412
  • 187
  • 1
  • 10
  • You've omitted the timezone at the end of your input. Without it JS will _guess_. Worse yet, different browsers will have different guesses. – VLAZ Nov 06 '16 at 10:54
  • 2
    Getting year 2075 would be ... surprising though. – trincot Nov 06 '16 at 10:58
  • @trincot totally missed the year... I was looking at the hours/minutes as that's usually the problem – VLAZ Nov 06 '16 at 10:58
  • How are you creating the date based on your string? If I do `var d = new Date('2015-12-08T13:42')` and then toString that date I get `Tue Dec 08 2015 13:42:00 GMT+1100 (AEDT)`. – JVDL Nov 06 '16 at 10:58
  • I'm getting `Tue Dec 08 2015 14:42:00 GMT+0100`, seems highly unlikely you'd get november 2075 ? – adeneo Nov 06 '16 at 10:59
  • 2
    Where exactly is this output coming from? Browser? in that case which one? Node.js? If so - which version? I tried it in Chrome and FF and I'm getting the expected result even if I try some variations. – VLAZ Nov 06 '16 at 11:02
  • 1
    Is your timezone set to 59 years in the future by any chance? – JVDL Nov 06 '16 at 11:03
  • Oh yes, I want to be in that timezone! Just to have a peek. Please... – trincot Nov 06 '16 at 11:06
  • @vlaz—the timezone is never guessed. For an ISO 8601 string, either the provided timezone is used, or if none, then the system timezone. – RobG Nov 06 '16 at 20:30
  • @RobG Either one is used but which one _is a guess_. Also, you forgot another one. See here: doing `new Date("2016-07-10T10:00:00")` in Edge produces 09:00Z (10:00+0100) while adding a `Z` at the end produces 10:00Z (11:00+0100). Chrome produces the exact same time for both of these and its 10:00Z (11:00+0100). Firefox, like Edge, gives me 09:00Z if I don't add a timezone, and 10:00Z if I explicitly do add Zulu in the input. If the timezone is never guessed, then I would have received the same thing for all browsers. However, the third timezone is Daylight savings. – VLAZ Nov 06 '16 at 20:45
  • @vlaz—the language specification states how ISO 8601 strings with and without time zones should be parsed, there is no guessing. However, for non–ISO strings, an implementation can use whatever heuristics it wants and it seems that is the case for Chrome. The general rule is to not use the Date constructor (or Date.parse) for parsing strings, you've provided more examples of why that is good advice. ;-) – RobG Nov 06 '16 at 20:59
  • @RobG Those were all parsing an ISO8601 string with and without a timezone leading to different results. Different browsers decided to use a different timezone offset for a date that was in summer time. – VLAZ Nov 06 '16 at 21:07
  • @vlaz—yes, as I keep saying, parsing using the Date constructor is unreliable. A date–only string *should* be treated as UTC (which is not consistent with ISO 8601), an *exact* ISO 8601 extended format date and time with a time zone *should* be treated as having that time zone. Remove the zone and it *should* use host settings. No guessing. But if it is not an *exact* ISO 8601 extended format date, the browser can do what it likes (which is what Chrome is doing). – RobG Nov 06 '16 at 23:59

3 Answers3

1

To specify UTC time as string, you need to add the Z part to it.

Quoted from Wikipedia:

If no UTC relation information is given with a time representation, the time is assumed to be in local time.

If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset.

I see that difference when running the following snippet in Firefox and in timezone GMT+01:00. But Edge and Chrome show the first output also as if the Z was there (which seems contrary to the above quotes), and so there is no difference visible:

var s = '2015-12-08T13:42';

console.log(new Date(s).toString());
console.log(new Date(s+'Z').toString());

Make sure the way you turn the Date value into string is correct. The toString() call (used in the snippet) will do this conversion into the current locale's date/time.

But if you use a custom function for that, you need to be aware of some pitfalls. For instance, if you use the getMonth or getUTCMonth methods to retrieve the month number of the given date, be aware that it is zero-based, and the month December is thus numbered 11. If your string conversion interprets this 11 as to mean November, you'll be one month off.

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
  • I'm using GMT so there is no difference for me...:( – VLAZ Nov 06 '16 at 11:06
  • I don't think any amount of timezone is going to get the input `2015-12-08T13:42` to be 59 years in the future (and even if the 75 is a typo for whatever reason, Nov 12 != Dec 8). – JVDL Nov 06 '16 at 11:07
  • @Dymos yeah, I'm getting more and more baffled by that output. I'm starting to think OP might have copy/pasted the wrong thing there and wasn't the result of the input given. – VLAZ Nov 06 '16 at 11:12
  • I'm assuming 2075 was a typo. A pity the OP left the scene right after posting. According to the output provided in the question, the OP is on GMT+1, so they will see the difference in this snippet's output. – trincot Nov 06 '16 at 11:15
  • I'm on GMT+1, and I don't see the difference, they both return the same for me ? – adeneo Nov 06 '16 at 11:40
  • I'm on GMT+1 too and they return two values that have one 1 hour difference. But that is in Firefox. I see Chrome and Edge interpret the first case also as a UTC string. Not so in Firefox, which seems therefore more in line with the things said in Wikipedia. – trincot Nov 06 '16 at 12:29
0

i think you u set the date to setUTCDate function if you pass the current date to setUTCDate function it get wrong date

        <!DOCTYPE html>
        <html>
        <body>

        <p id="demo"></p>

        <script>
        function myFunction() {
            var d = new Date();
            d.setUTCDate(2015-12-08);
            document.getElementById("demo").innerHTML = d;
        }
        </script>

        </body>
        </html>

if you want get current date use  Date() function

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>

document.getElementById("demo").innerHTML = Date();
</script>

</body>
</html>
0

I thought I had a standard ISO date and the Javascript Date() function should convert it with no parsing,

To convert a string to a Date, it must be parsed, you can't avoid that.

but I get the following: my date : 2015-12-08T13:42

Javascript date function result:

Tue Nov 12 2075 13:42:00 GMT+0100

Is my date in an incorrect format or have I misunderstood the Data function?

Apart from the year, that is the expected result. From ES5, ISO 8601 format date strings are parsed by the Date constructor (and Date.parse, they are equivalent for parsing).

A plain date string (e.g. 2015-12-08) should be parsed as UTC. A date and time should be parsed as either local if no timezone is provided, or using the provided timezone.

The string "2015-12-08T13:42" does not have a timezone, so it will be parsed using the host timezone settings to produce a date that is equivalent to a local date and time of 8 December, 2015 at 1:42 pm. So it will represent a different moment in time in each time zone with a different offset.

Parsing of strings with the Date constructor is strongly discouraged as it is largely implementation dependent, either write a simple function or use a library. You don't say whether you want the string parsed as local or UTC, a simple function to parse it as UTC is:

// 2015-12-08T13:42

function parseISOAsUTC(s) {
  var b = s.split(/\D/);
  return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5]||0));
}

var d = parseISOAsUTC('2015-12-08T13:42');
console.log('Local: ' + d.toLocaleString() +
            '\nUTC:   ' + d.toISOString());

You should add some validation to deal with out of bounds values.

RobG
  • 142,382
  • 31
  • 172
  • 209