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!

1

2 Answers 2

4

2018-03-03T23:10:24.063Z

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, do you know the logic for Javascript changing the formatting when concatenating in a string? I don't really like it :|
@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)
Wondering where the selection between toString and toISOString is being made. Switch in console.log() perhaps
@Tibrogargan I'm not sure that I understand your question, but how stuff is logged is up to the Printer implementation. Nothing requires that toString is called.
@Tibrogargan check the answer below, internally toString() is called 🤔
|
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

5 Comments

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: github.com/whatwg/console/issues/132
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!
Yep. Causes headaches, but hopefully not much more than that. ;)
If ISO format is required, then toISOString seems to be a much more semantic option.
so console.log() calls .toISOString() and when concating a string which includes a date toString() is called is that correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.