3

const date = new Date();

date.setDate(date.getDate() - 30);

console.log(date); // 2018-03-03T23:10:24.063Z 
console.log(date + 'hello'); // Sat Mar 03 2018 15:10:59 GMT-0800 (PST)hello

What's going on here? How can I use the date value without formatting it to be human readable? Thanks!

connexo
  • 53,704
  • 14
  • 91
  • 128
Anthony
  • 13,434
  • 14
  • 60
  • 80
  • The console is stringifying your date differently depending on how you print it. You can use a Date however you like, see [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Apr 03 '18 at 04:18

2 Answers2

4

2018-03-03T23:10:24.063Z

This is date.toISOString(), so date.toISOString() + 'hello'.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thanks, do you know the logic for Javascript changing the formatting when concatenating in a string? I don't really like it :| – Anthony Apr 02 '18 at 22:15
  • 2
    @Antoine - It's not changing any format. It's just that `console.log` on a `Date` object is not defined by any spec whatsoever, and thus varies across implementations. (In the first log statement, you are logging a `Date`. In the second one, you're logging a `String`) – Matt Johnson-Pint Apr 02 '18 at 22:16
  • 2
    Wondering where the selection between `toString` and `toISOString` is being made. Switch in `console.log()` perhaps – Tibrogargan Apr 02 '18 at 22:19
  • @Tibrogargan I'm not sure that I understand your question, but how stuff is logged is up to the [Printer](https://console.spec.whatwg.org/#printer) implementation. Nothing requires that `toString` is called. – pushkin Apr 02 '18 at 22:25
  • @Tibrogargan check the answer below, internally toString() is called – Anthony Apr 02 '18 at 22:27
  • @Tibrogargan - it's not a switch per se, it's just that it differs in the underlying implementations, because it's not standardized. I've asked for it to be, here: https://github.com/whatwg/console/issues/132 – Matt Johnson-Pint Apr 02 '18 at 22:38
  • @MattJohnson On the face of it, that's not correct. The `+` operator in JavaScript is converting the date to a string through `Object.toString` and getting the expected result. Something in the `console.log` call stack however is aware of the `Date` class and selecting `toISOString` as opposed to just using the default `Object.toString` – Tibrogargan Apr 02 '18 at 23:33
  • Yes, agreed. We are saying the same thing. The behavior is defined for concatenation, but not for logging. – Matt Johnson-Pint Apr 02 '18 at 23:35
2

toJSON() is your friend (more often than not):

const date = new Date();

date.setDate(date.getDate() - 30);

console.log(date);
console.log(date.toString());
console.log(`${date.toJSON()}hello`);

Internally, Date.prototype.toJSON() uses Date.prototype.toISOString().

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON

When concatenating a date object with a string, internally Date.prototype.toString() is being called - and that creates the output you do not want in your case.

The Date object overrides the toString() method of the Object object; it does not inherit Object.prototype.toString(). For Date objects, the toString() method returns a string representation of the object.

The toString() method always returns a string representation of the date in American English.

JavaScript calls the toString() method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString

connexo
  • 53,704
  • 14
  • 91
  • 128
  • 2
    Unfortunately, there's nothing that aligns the output of `console.log` with the output of `toJSON`, or that says `console.log` should call `.toString()` or `.toISOString()` on a `Date` object. I've requested it here: https://github.com/whatwg/console/issues/132 – Matt Johnson-Pint Apr 02 '18 at 22:34
  • Well, as long as that's only a problem for console output, it's not too big of an issue - other than frequently producing SO questions. Good point nonetheless! – connexo Apr 02 '18 at 22:38
  • Yep. Causes headaches, but hopefully not much more than that. ;) – Matt Johnson-Pint Apr 02 '18 at 22:39
  • If ISO format is required, then *toISOString* seems to be a much more semantic option. – RobG Apr 03 '18 at 04:22
  • so console.log() calls ```.toISOString()``` and when concating a string which includes a date ```toString()``` is called is that correct? – zlZimon Sep 18 '19 at 14:43