search

Home  >  Q&A  >  body text

javascript - How to imitate moment.js object and change the words printed by console.log?

When I use moment.js, in the environment of Node.js v7

var moment = require('moment');
console.log(moment());

will be printed in the console

moment("2017-06-23T09:06:56.216")

use

console.log(typeof moment());

Learn that moment() is an object

Sometimes, moment.js will also add some prompt words to the console.log, such as when using moment-timezone but not specifying the timezone of the second parameter

console.log(moment.tz('2013-06-01T00:00:00-04:00'));

will be printed on the console

Moment Timezone has no data for 2013-06-01T00:00:00-04:00. See http://momentjs.com
/timezone/docs/#/data-loading/.

moment.utc("2017-06-23T01:12:20.003+00:00")

Above, how to set the text printed by a javascipt object when console.log this object?

ringa_leeringa_lee2751 days ago962

reply all(3)I'll reply

  • 黄舟

    黄舟2017-06-26 10:54:46

    Because the inspectmethod

    has been rewritten

    You can try it under node:

    console.log({inspect: function () { return 'this is my value'}})

    Or you can try:

    moment().inspect() // moment("2017-06-23T09:06:56.216")

    reply
    0
  • typecho

    typecho2017-06-26 10:54:46

    Recommend an article
    https://shijianan.com/2017/04...

    reply
    0
  • 迷茫

    迷茫2017-06-26 10:54:46

    Just override the toString method.

    reply
    0
  • Cancelreply