Date's constructor doesn't behave the same on trimmed and untrimmed strings [duplicate]Why does Date.parse give incorrect results?Trim string in JavaScript?JavaScript chop/slice/trim off last character in stringHow to trim a file extension from a String in JavaScript?Inconsistant date parsing with missing timezoneCorrectly format times between client (javascript) and the server (PHP)Javascript date string passed to Date constructor gives strange resultsMongoose/MongoDB regex find query using variableGoogle Maps Time Zone API get local time in UTCCreate moment.js date from milliseconds in local timezoneIs there a workaround in JSON Parse and Stringify for Dates when a custom engine doesn't accept the norm

How to store my pliers and wire cutters on my desk?

How does a poisoned arrow combine with the spell Conjure Barrage?

Why put copper in between battery contacts and clamps?

What do I lose by going Paladin 17 / Warlock 3

How to efficiently shred a lot of cabbage?

What language is Raven using for her attack in the new 52?

Desktop app status bar: Notification vs error message

What are the closest international airports in different countries?

Who said "one can be a powerful king with a very small sceptre"?

Self-deportation of American Citizens from US

Complaints from (junior) developers against solution architects: how can we show the benefits of our work and improve relationships?

How can Paypal know my card is being used in another account?

What is an Accessible Word?

How well would the Moon protect the Earth from an Asteroid?

Why does Canada require bilingualism in a lot of federal government posts?

How do I make my photos have more impact?

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

What would the United Kingdom's "optimal" Brexit deal look like?

How did astronauts using rovers tell direction without compasses on the Moon?

Unknown indication below upper stave

Why is it "on the inside" and not "in the inside"?

What is the German equivalent of the proverb 水清ければ魚棲まず (if the water is clear, fish won't live there)?

Composing fill in the blanks

To find islands of 1 and 0 in matrix



Date's constructor doesn't behave the same on trimmed and untrimmed strings [duplicate]


Why does Date.parse give incorrect results?Trim string in JavaScript?JavaScript chop/slice/trim off last character in stringHow to trim a file extension from a String in JavaScript?Inconsistant date parsing with missing timezoneCorrectly format times between client (javascript) and the server (PHP)Javascript date string passed to Date constructor gives strange resultsMongoose/MongoDB regex find query using variableGoogle Maps Time Zone API get local time in UTCCreate moment.js date from milliseconds in local timezoneIs there a workaround in JSON Parse and Stringify for Dates when a custom engine doesn't accept the norm






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0
















This question already has an answer here:



  • Why does Date.parse give incorrect results?

    11 answers



I know that creating a date from string is usually a bad idea, but still, this caught my attention: adding a space before or after the date string can affect the created date value.






console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);





According to the Date docs, new Date(<string>) invokes Date.parse to get the time value. Besides that, the docs don't seem to give any pointers to what happens to untrimmed strings.



I'm really stuck on this one! Why do space affect the time? It's programming, not general relativity!




The console logs above where produced by a Chrome 73 browser running a v8 engine in Berlin (UTC+1)










share|improve this question














marked as duplicate by RobG date
Users with the  date badge can single-handedly close date questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 27 at 0:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

    – Bergi
    Mar 26 at 20:34












  • That seems indeed to be the answer. Still, that's a very unintuitive design

    – Nino Filiu
    Mar 26 at 20:41











  • Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

    – charlietfl
    Mar 26 at 20:46







  • 1





    It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

    – Nino Filiu
    Mar 26 at 20:49






  • 1





    @NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

    – Bergi
    Mar 26 at 20:57


















0
















This question already has an answer here:



  • Why does Date.parse give incorrect results?

    11 answers



I know that creating a date from string is usually a bad idea, but still, this caught my attention: adding a space before or after the date string can affect the created date value.






console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);





According to the Date docs, new Date(<string>) invokes Date.parse to get the time value. Besides that, the docs don't seem to give any pointers to what happens to untrimmed strings.



I'm really stuck on this one! Why do space affect the time? It's programming, not general relativity!




The console logs above where produced by a Chrome 73 browser running a v8 engine in Berlin (UTC+1)










share|improve this question














marked as duplicate by RobG date
Users with the  date badge can single-handedly close date questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 27 at 0:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

    – Bergi
    Mar 26 at 20:34












  • That seems indeed to be the answer. Still, that's a very unintuitive design

    – Nino Filiu
    Mar 26 at 20:41











  • Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

    – charlietfl
    Mar 26 at 20:46







  • 1





    It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

    – Nino Filiu
    Mar 26 at 20:49






  • 1





    @NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

    – Bergi
    Mar 26 at 20:57














0












0








0









This question already has an answer here:



  • Why does Date.parse give incorrect results?

    11 answers



I know that creating a date from string is usually a bad idea, but still, this caught my attention: adding a space before or after the date string can affect the created date value.






console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);





According to the Date docs, new Date(<string>) invokes Date.parse to get the time value. Besides that, the docs don't seem to give any pointers to what happens to untrimmed strings.



I'm really stuck on this one! Why do space affect the time? It's programming, not general relativity!




The console logs above where produced by a Chrome 73 browser running a v8 engine in Berlin (UTC+1)










share|improve this question















This question already has an answer here:



  • Why does Date.parse give incorrect results?

    11 answers



I know that creating a date from string is usually a bad idea, but still, this caught my attention: adding a space before or after the date string can affect the created date value.






console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);





According to the Date docs, new Date(<string>) invokes Date.parse to get the time value. Besides that, the docs don't seem to give any pointers to what happens to untrimmed strings.



I'm really stuck on this one! Why do space affect the time? It's programming, not general relativity!




The console logs above where produced by a Chrome 73 browser running a v8 engine in Berlin (UTC+1)





This question already has an answer here:



  • Why does Date.parse give incorrect results?

    11 answers






console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);





console.log([

new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z

new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z

new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z

]);






javascript date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 20:32









Nino FiliuNino Filiu

4,2825 gold badges17 silver badges34 bronze badges




4,2825 gold badges17 silver badges34 bronze badges





marked as duplicate by RobG date
Users with the  date badge can single-handedly close date questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 27 at 0:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











marked as duplicate by RobG date
Users with the  date badge can single-handedly close date questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 27 at 0:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by RobG date
Users with the  date badge can single-handedly close date questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 27 at 0:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

    – Bergi
    Mar 26 at 20:34












  • That seems indeed to be the answer. Still, that's a very unintuitive design

    – Nino Filiu
    Mar 26 at 20:41











  • Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

    – charlietfl
    Mar 26 at 20:46







  • 1





    It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

    – Nino Filiu
    Mar 26 at 20:49






  • 1





    @NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

    – Bergi
    Mar 26 at 20:57













  • 2





    Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

    – Bergi
    Mar 26 at 20:34












  • That seems indeed to be the answer. Still, that's a very unintuitive design

    – Nino Filiu
    Mar 26 at 20:41











  • Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

    – charlietfl
    Mar 26 at 20:46







  • 1





    It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

    – Nino Filiu
    Mar 26 at 20:49






  • 1





    @NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

    – Bergi
    Mar 26 at 20:57








2




2





Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

– Bergi
Mar 26 at 20:34






Looks like only strict formats are considered as UTC times, and sloppy formats (such as including a space) are parsed according to the local timezone.

– Bergi
Mar 26 at 20:34














That seems indeed to be the answer. Still, that's a very unintuitive design

– Nino Filiu
Mar 26 at 20:41





That seems indeed to be the answer. Still, that's a very unintuitive design

– Nino Filiu
Mar 26 at 20:41













Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

– charlietfl
Mar 26 at 20:46






Why would spaces in date strings be considered intuitive? Validate your input first. Note that not all of those formats will work consistently cross browser also

– charlietfl
Mar 26 at 20:46





1




1





It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

– Nino Filiu
Mar 26 at 20:49





It's not the spaces that are unintuitive, it's the fact that JS decides to chose a different time zone based on the sloppiness of the input. They shouldn't be linked together IMO

– Nino Filiu
Mar 26 at 20:49




1




1





@NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

– Bergi
Mar 26 at 20:57






@NinoFiliu Well yes, the design of the parser isn't really good, but that how it is and can hardly be changed for webcompat reasons. And this is the exact (and only) reason why using Date.parse to create dates from strings is usually a bad idea in JavaScript (that is, unless you include a timezone signifier explicitly).

– Bergi
Mar 26 at 20:57













1 Answer
1






active

oldest

votes


















2














From the specification (paragraph 20.3.3.2):




The function first attempts to parse the format of the String
according to the rules (including extended years) called out in Date
Time String Format (20.3.1.16). If the String does not conform to that
format the function may fall back to any implementation-specific
heuristics or implementation-specific date formats.




So, when a space is added, the string does not conform to the Date Time String Format and the parser falls back to implementation-specific algorithm. Like Bergi writes, this takes the local timezone in account.






share|improve this answer

























  • "… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

    – RobG
    Mar 27 at 6:22














1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














From the specification (paragraph 20.3.3.2):




The function first attempts to parse the format of the String
according to the rules (including extended years) called out in Date
Time String Format (20.3.1.16). If the String does not conform to that
format the function may fall back to any implementation-specific
heuristics or implementation-specific date formats.




So, when a space is added, the string does not conform to the Date Time String Format and the parser falls back to implementation-specific algorithm. Like Bergi writes, this takes the local timezone in account.






share|improve this answer

























  • "… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

    – RobG
    Mar 27 at 6:22















2














From the specification (paragraph 20.3.3.2):




The function first attempts to parse the format of the String
according to the rules (including extended years) called out in Date
Time String Format (20.3.1.16). If the String does not conform to that
format the function may fall back to any implementation-specific
heuristics or implementation-specific date formats.




So, when a space is added, the string does not conform to the Date Time String Format and the parser falls back to implementation-specific algorithm. Like Bergi writes, this takes the local timezone in account.






share|improve this answer

























  • "… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

    – RobG
    Mar 27 at 6:22













2












2








2







From the specification (paragraph 20.3.3.2):




The function first attempts to parse the format of the String
according to the rules (including extended years) called out in Date
Time String Format (20.3.1.16). If the String does not conform to that
format the function may fall back to any implementation-specific
heuristics or implementation-specific date formats.




So, when a space is added, the string does not conform to the Date Time String Format and the parser falls back to implementation-specific algorithm. Like Bergi writes, this takes the local timezone in account.






share|improve this answer













From the specification (paragraph 20.3.3.2):




The function first attempts to parse the format of the String
according to the rules (including extended years) called out in Date
Time String Format (20.3.1.16). If the String does not conform to that
format the function may fall back to any implementation-specific
heuristics or implementation-specific date formats.




So, when a space is added, the string does not conform to the Date Time String Format and the parser falls back to implementation-specific algorithm. Like Bergi writes, this takes the local timezone in account.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 20:51









edwinedwin

2,15915 silver badges15 bronze badges




2,15915 silver badges15 bronze badges















  • "… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

    – RobG
    Mar 27 at 6:22

















  • "… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

    – RobG
    Mar 27 at 6:22
















"… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

– RobG
Mar 27 at 6:22





"… this takes the local timezone in account" that is not part of the specification. 2019-03-05 should be parsed as local, but the OP's results show it's being parsed as UTC. So even when the string does conform to ECMA-262, some parsers still get it wrong. And if it doesn't conform to ECMA-262, it's not necessarily parsed as local or UTC, it's up to the implementation. ;-)

– RobG
Mar 27 at 6:22








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현