How does console.log convert javascript date object into human readable format

Related searches

Output of console.log({a:new Date()}) is { a: 2019-05-19T11:30:57.514Z }

Value of JSON.stringify({a:new Date()}) is {"a":"2019-05-19T11:33:12.591Z"}

After overriding this: Date.prototype.toJSON = function(){ return this.toLocaleString(); }

Value of JSON.stringify({a:new Date()}) is {"a":"5/19/2019, 5:09:31 PM"}

But output of console.log({a:new Date()}) is still { a: 2019-05-19T11:41:31.256Z }

Tried overriding other Date.prototype methods like toISOString(), toSource, toString, toUTCString, valueOf and many others. But none helped.

Couldn't understand the native source code of v8 js engine.

Any way to override the behavior to get the desired result?

It depends on the console implementation, which isn't in V8 but rather in the host (Chrome, Chromium, Node.js, ...).

Node.js used to look for a member called inspect (back in v4 at least) and use that if it was present, but that turned out to be a compatibility problem and it doesn't do that anymore.

I don't think there's any way to override the console's rendering of built-in objects anymore, particularly not across host environments.

Date.prototype.toTimeString(), Then, you can convert it to any format that you like. Javascript They are: toDateString: Converts the date portion of a Date object into a readable string; toGMTString: Deprecated. toISOString()); # 2020-08-20T00:00:00.044Z console.log(ts. I'd like to display some dates as relative to the current date in a human-friendly format. Examples of human-friendly relative dates: 10 seconds ago 20 minutes from now 1 day ago 5 weeks ago 2

The console uses toString to convert dates (and other objects) to string for printing. So you can override Date.prototype.toString to change what gets printed. Demo (copy-pasted from a Chrome console session):

> var d = new Date()
> console.log(d)
< Sun May 19 2019 14:29:02 GMT+0200 (Central European Summer Time)
> Date.prototype.toString = Date.prototype.toLocaleString
< ƒ toLocaleString() { [native code] }
> console.log(d)
< 5/19/2019, 2:29:02 PM
> console.log({a:new Date()})
< {a: 5/19/2019, 2:31:21 PM}

A separate issue is that JSON.stringify({a: 1}) gives {"a":1}, whereas console.log({a: 1}) prints {a: 1} (without quotes around a). That's unrelated to dates, and is due to the fact that JSON is a very limited subset of JavaScript ({a: 1} is valid JavaScript but not valid JSON). Also, console.log is intended for human consumption, there is no requirement that the output can be parsed back to the original object(s) -- just consider what console.log(new Array(100)) does :-)


Edit: The above is true for Chrome. Since the console object is not covered by the ECMAScript spec, every embedder can provide whatever they think is useful there. Node.js uses util.format, which in turn uses util.inspect, which in turn gets the toISOString function from the primordial Date.prototype, which means you can't override it (which is a feature because that means you can't break it, or a limitation because it means you can't customize it, depending on your point of view).

How To Get Current Date And Time in JavaScript, Unlike other languages, JavaScript doesn't have equivalent for function strftime . toDateString: Converts the date portion of a Date object into a readable string a string, using the ISO standard; toJSON: Returns the date as a string, formatted as a JSON date toISOString()); # 2020-09-10T00:00:00.044Z console .log(ts. Output: Using toDateString(): This method returns date portion of Date object in human readable form. Example-3:

You are not converting the new date() into json when you are doing the console.log(). You can write it this way.

Date.prototype.toJSON = function(){ return this.toLocaleString(); }
console.log({a:JSON.stringify(new Date())})

How To Convert Timestamp To Date and Time in JavaScript, Use new Date() to generate a new Date object containing the current date and Once you have a date object, you can apply any of the several available methods to Convert the current time and date to a human-readable string. var now = new Date(); // convert date to a string in UTC timezone format: console.log(now. Moment is the de facto choice for converting dates to neatly formatted strings in JavaScript, although some people opt out of using Moment to reduce bundle size. Formatting Dates. Moment's format() method is what you use to convert a Moment object to a string. For example, here's how you would convert a YYYY-MM-DD string into a more human

JavaScript, Convert today's date into a readable string: var d = new Date The toDateString () method converts the date (not the time) of a Date object into a readable string. The simplest way to convert your date to the yyyy-mm-dd format, is to do this: var date = new Date("Sun May 11,2014"); var dateString = new Date(date.getTime

JavaScript toDateString() Method, See the Pen JavaScript - Convert a Unix timestamp to time-date-ex- 17 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus Previous: Write a JavaScript function to count the number of days passed since beginning of the year.

Convert time. To output time in a human-readable format, you first have to convert it to moment object. If you are getting the timestamp from server, it has to be an ISO-compliant date string, or

Comments
  • json is not object, and imho diff them in console is a good thing.
  • if you want json representation, use console.log(JSON.stringify(...))
  • You didn't actually specify what "the desired result" is.
  • You are correct, but that's true only for chrome. It doesn't work in nodejs that way.
  • "The console uses toString" not necessarily. In the OP, it seems to be using toISOString or perhaps JSON.stringify. The console is a host object, its behaviour is not defined by a standard like ECMA-262 so really it can represent dates any way it wants.
  • Yup, console is not spec'ed. I've added a paragraph about Node (the OP didn't specify which environment it cared about).
  • The whole point of the question seems to be about overriding what console.log does with a Date object, not a string.