-3

Works:

const date = new Date();
temp = date.toISOString();

Doesn't work:

const date = new Date();
temp = date.setDate(date.getDate() - date.getDay()).toISOString();

Works too:

const date = new Date();
temp = date.setDate(date.getDate() - date.getDay()).toString();

What may be the reason for toISOString() not working?

MightyA
  • 26
  • 6
  • 5
    `setDate` doesn't return a Date object, it returns a number. So a number has a toString, but it doesn't have a toISOString. – Keith Sep 07 '22 at 12:49
  • 3
    `setDate` returns a number, not a `Date`. Every type has a `toString` method, only `Date`s have a `toISOString`. – deceze Sep 07 '22 at 12:49
  • So how do I convert the number (which is actually a date as a number) into a date? toDate doesn't seem to work! – MightyA Sep 07 '22 at 14:06
  • 1
    `date.setDate(...); temp = date.toISOString()`. You simply can't *chain* these two actions. – deceze Sep 07 '22 at 14:09
  • I didn't recognize that. I'm so sorry. Now, I see. Thank you very much. – MightyA Sep 07 '22 at 15:25
  • How may I improve this question (in order to get back asking priviliges)? I don't have a clue. :( – MightyA Nov 20 '22 at 15:43

1 Answers1

0

Because .toString() can be called on almost any value while .toISOString() can only be called on Date objects1.

This code:

const date = new Date();
temp = date.setDate(date.getDate() - date.getDay()).toISOString();

is effectively the same as

const date = new Date();
const setDateReturnValue = date.setDate(date.getDate() - date.getDay());
setDateReturnValue.toISOString();

Note that date and setDateReturnValue hold two different values. The date is the Date object and setDateReturnValue is the return value of Date.prototype.setDate(), which is a number.

A number doesn't have a .toISOString() method, which is what is causing the error. A Date does have a .toISOString() method, which is why your first code snippet works fine.

Both a Date and a number have a .toString() method2, which is why those work fine, but do realize that the string they return is different in a different format. (The number will return the number as a string, where the date will return something like "Wed Sep 07 2022 16:17:40 GMT+0200 (Central European Summer Time)")

[1]: Unless of course you or a library defines this function for different types.
[2]: Technically speaking: a number a primitive and as such, doesn't have a .toString() method (or any method for that matter), but when calling a method on a number, it will be boxed into a Number object, which does have a .toString() method.

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • When I try your code, it's telling me: setDateReturnValue.toISOString is not a function. What am I doing wrong? – MightyA Sep 08 '22 at 12:23
  • @MightyA That's what I said in the answer. "_`setDateReturnValue` is the return value of `Date.prototype.setDate()`, which is a number_" and "_A number doesn't have a `.toISOString()` method, which is what is causing the error._". If you want to call `.toISOString()`. you'll have to call it on `date`. – Ivar Sep 08 '22 at 12:26