-1

I have a problem regarding the JavaScript date function.

var atime = new Date("07.08.2021");
var btime = new Date("19.08.2021");
var ctime = new Date("03.10.2021");
var today = new Date();

console.log(atime);
console.log(btime);

The variables "atime", "ctime" and "today" are working perfectly fine, but btime does not. It always says "Invalid Date", but why, it is completely the same as atime, or ctime?

It does not work with new Date("08/19/2021") either?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Flo
  • 29
  • 3
  • 1
    I... very much doubt that? Open your dev console, and paste `new Date("07.08.2021");` into it. That should immediately tell you that's an invalid date, because that's not a legal date string as far as JS is concerned. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#parameters point 3. – Mike 'Pomax' Kamermans Jan 11 '22 at 21:24
  • 1
    Not exactly your situation, but shows how you need to consider how to take a non-valid date string and convert it into one [How to parse a date in format "YYYYmmdd" in JavaScript?](https://stackoverflow.com/questions/10638529/how-to-parse-a-date-in-format-yyyymmdd-in-javascript) – Kinglish Jan 11 '22 at 21:24
  • 2
    @Mike'Pomax'Kamermans It likely depends on what locale / browser the user is using as `07.08.2021` in UK locale on Chrome is a valid date. Saying that the only valid specification based date constructor is `YYYY-MM-DD` so all the dates the OP is using are incorrect. – Keith Jan 11 '22 at 21:34
  • As I said, the other variables work fine with DD-MM-YYYY. So why does this single one not? – Flo Jan 11 '22 at 21:38
  • 1
    The other dates "work fine" because their day value is low enough to be a valid month value. However the first date (atime) is logged as july 8, not aug 7, at least in my browser. Is that what you expected? – James Jan 11 '22 at 21:42
  • @Keith certainly, hence the link to the Date constructor documentation. Whether they read the whole thing or not is not up to me, but I hope they do. – Mike 'Pomax' Kamermans Jan 11 '22 at 22:19

2 Answers2

1

Refer to documentation of Date at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object that warns for parsing strings

Note: Parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

Prefer this kind of notation new Date(2021, 8, 19)

In addition, I would strongly recommend you invest some time to consider using Moment.js, e.g. https://momentjs.com/docs/#/parsing/string-format/

Fred Klein
  • 448
  • 6
  • 13
1

As stated here you should only use one of the 3 date formats:

  1. ISO Date: "YYYY-MM-DD" (The International Standard)
  2. Short Date: "MM/DD/YYY"
  3. Long Date: "Mar 25 2015" or "25 Mar 2015"

Other formats might get interpreted different depending on which browser you are using. Switching your date format to Short Date would probably fix your Problem

Specht
  • 50
  • 10