-1
// javascript
var date1 = new Date('1/1/19');
var y = date1.getYear();
var fy = date1.getFullYear();

When I run the code I get (Windows 10, VS2015, MVC5, Jquery 3.3.1, Bootstrap 4.1:

y = '19'
fy = '1919'

Per spec I should get:

y = "119"
fy = "2019"

I did go to different web sites and I run the code above and I get the right result but not from my computer.

Can any one confirm this issue or is it just my computer issue?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
D052057
  • 53
  • 6
  • Welcome to Stack Overflow. When you say "per spec", which spec are you referring to, in terms of behaviour when calling `new Date()` with a string which *isn't* ISO-8601 format? – Jon Skeet Feb 15 '19 at 07:16
  • I did test it with Chrome and IE edge browsers and they gave right results but not IE 11 (bug - give me wrong year). – D052057 Feb 15 '19 at 07:26
  • When year is 119, it means 2019. When year is 19 it means 1919. That is the spec I am talking about. I supposed to get 119 instead of 19. This is bad for IE 11. – D052057 Feb 15 '19 at 07:27
  • 1
    "When year is 19 it means 1919" - yes, and that's entirely consistent with the date you're creating *being 1919, not 2019*. That's why you're seeing `fy = '1919'`. That's why I asked the question about the spec relating to how you expect `new Date('1/1/19')` to behave - what makes you think it's guaranteed to return January 1st 2019 rather than January 1st 1919? (If you used `2019-01-01` I suspect you'd get the expected result from all browsers.) – Jon Skeet Feb 15 '19 at 07:50
  • "1/1/19" might parsed as 0019-01-01 or 1919-01-01 or 2019-01-01 or any century you like. – RobG Feb 15 '19 at 21:08
  • Moment.js will fix the issue. Work with IE11, Chrome, Firefox. I have installed and tested. New Date('1/1/19') return year is 2019 - that is correct. – D052057 Feb 16 '19 at 22:50

1 Answers1

1

You can find some info here. E.g., you can find that

Previous Century - One and two digit years will be interpreted as 19xx:

Full spec of date input format is available here.

Briefly speaking, in order to obtain 2019 from getFullYear() calls you should initialize Date instances providing something like 01/01/2019.

IMHO it's useful, in order to avoid problems and misunderstandings with timezones, adopt a simple, safe strategy: referring to dates using ISO 8601 format, which is supported by Date(String) constructor.

For advanced uses you can interact with dates through a library like moment.js, which provides a custom, simple and usable API over standard date APIs (and provides an extension for explicitly handling timezones, if it's the case).

Pietro Martinelli
  • 1,776
  • 14
  • 16
  • 2
    "you should initialize Date instances providing something like 01/01/2019" - I'd **only** recommend ISO-8601 instead, which is unambiguous: "2019-01-01". I wouldn't be surprised if using "02/01/2019" produced different results on different browsers, for example. (Some giving February 1st, some giving January 2nd.) – Jon Skeet Feb 15 '19 at 07:52
  • It is a bug in IE 11 (Javascript version 1.3). I have not tried momen.js. New Date("1/1/19") to new Date('1/1/49') will produce 3 digits for date.getYear() which is 2019 - 2049. New Date('1/1/50') to New Date('1/1/99') gave just 2 digits instead which means that I will get 1950 to 1999. IE 11 Is all WRONG. – D052057 Feb 15 '19 at 20:03
  • Using ISO 8601 format is not a safe strategy either, at least one modern browser incorrectly parses ISO 8601 formatted dates and many old browsers don't parse them at all. – RobG Feb 15 '19 at 21:06
  • Installed moment.js and test it with IE 11, Edge, Chrome and Firefox and I get desired results (work with 2 digits year and of course with 4 digits year). – D052057 Feb 16 '19 at 22:36