Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moment().fromNow() returns 'Invalid date' #4802

Closed
OregonianPyro opened this issue Oct 7, 2018 · 10 comments
Closed

moment().fromNow() returns 'Invalid date' #4802

OregonianPyro opened this issue Oct 7, 2018 · 10 comments
Labels

Comments

@OregonianPyro
Copy link

@OregonianPyro OregonianPyro commented Oct 7, 2018

Description of the Issue and Steps to Reproduce:
I'm using moment to get the age of a user's account, by using:
moment(user account created date here).fromNow() but it return 'Invalid date'. This worked before but it no longer works. The created date looks something like: 2018-06-07T22:54:07.310Z.
Using moment(date here).format() works fine.
Please include the values of all variables used.
The date I use to format and use fromNow is 2018-06-07T22:54:07.310Z
Environment:
Discord Bot
Examples: Chrome 49 on OSX, Internet Explorer 10 on Windows 7, Node.JS 4.4.4 on Ubuntu 16.0.4
I don't use moment for anything listed above.
Both the browser and the OS are important to us, particularly if you have an unsual environment like an IOT application.

Other information that may be helpful: https://i.imgur.com/4LFsZh3.png

  • The time zone setting of the machine the code is running on: PST
  • The time and date at which the code was run: 12:20PM, 10/7/18
  • Other libraries in use (TypeScript, Immutable.js, etc) None

If you are reporting an issue, please run the following code in the environment you are using and include the output:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)

Sun Oct 07 2018 12:29:05 GMT-0700 (Pacific Daylight Time)
2018-10-7 12:29:05
420
2.22.2
Ensure your issue is isolated to moment. Issues involving third party tools will be closed unless submitted by the tool's author/maintainer.

@ashsearle
Copy link
Contributor

@ashsearle ashsearle commented Oct 8, 2018

How do you run your Discord Bot?

You said: "This worked before but it no longer works"
Has anything changed since it worked - e.g. have you updated the environment your discord bot runs on? Has the format of 'user account created date' changed?

Are you able to modify your discord bot code to show the output of the below (specifically using hard-coded strings):

console.log('moment.format:', moment("2018-06-07T22:54:07.310Z").format());
console.log('moment.fromNow:', moment("2018-06-07T22:54:07.310Z").fromNow());
@OregonianPyro
Copy link
Author

@OregonianPyro OregonianPyro commented Oct 12, 2018

It was working before then just stopped. No code changed. No envirnoment changed with the bot. The way the dates are being parsed seem the same too.

And here's what the console outputted;

moment.format: 2018-06-07T15:54:07-07:00
moment.fromNow: Invalid date

and here's how I used it (this.client.moment is how I have moment defined):

 console.log('moment.format:', this.client.moment("2018-06-07T22:54:07.310Z").format());
console.log('moment.fromNow:', this.client.moment("2018-06-07T22:54:07.310Z").fromNow());

Not sure if I mentioned it or not, but here are the current versions I'm using:
"moment": "^2.22.2",
"moment-duration-format": "^2.2.2"
and I'm on v8.9.2 of node

@manofspirit
Copy link

@manofspirit manofspirit commented Nov 3, 2018

I'm having similar issue with Angular 6 (ionic based app) and moment 2.22.2 returns invalid date for something as simple as this.
moment(new Date()).fromNow();
please advise.

@jamesastound
Copy link

@jamesastound jamesastound commented Jan 15, 2019

Still happening in 2.23.0

time = moment('2017-08-08T18:29:07.000Z')
time.isValid() // true
time.toNow() // Invalid date
time.fromNow() // Invalid date

@marwahaha
Copy link
Member

@marwahaha marwahaha commented Jan 17, 2019

Thanks for opening this request
Hi! Thanks for filing this issue! Unfortunately, I can't reproduce this. Could you provide any other information?

It helps if you run these lines on your machines:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)
@jamesastound
Copy link

@jamesastound jamesastound commented Jan 17, 2019

Thanks for the reply. I am using NodeJS, so there is not a navigator environment.

new Date()).toString() Thu Jan 17 2019 06:53:11 GMT-0600 (Central Standard Time)
new Date()).toLocaleString(): 1/17/2019, 6:53:11 AM
new Date()).getTimezoneOffset(): 360
moment.version: 2.23.0
NodeJS is v11.6.0

I've been doing some testing and the conflict is an Object.prototype constructor.

Object.prototype.test = function () {
    return test
}

const moment = require('moment');

const time = moment('2017-08-08T18:29:07.000Z')
console.log(time.isValid()) // true
console.log(time.toNow()) // Invalid date
console.log(time.fromNow()) // Invalid date

@XenHat
Copy link

@XenHat XenHat commented Mar 21, 2019

I am also affected by this.
At first, I believed that using an incomplete unix timestamp (how the data is provided to me) in the constructor was the cause, but upon testing the example code in MomentJS' documentation, I found that any call to fromNow() returned Invalid Date.

@ramsy-leftclick
Copy link

@ramsy-leftclick ramsy-leftclick commented Jan 7, 2020

Same problem here.

toString Tue Jan 07 2020 10:20:09 GMT+0100 (Central European Standard Time)
toLocaleString 1/7/2020, 10:20:09 AM
getTimezoneOffset -60
navigator.userAgent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0
moment.version 2.24.0
@Pranit-Harekar
Copy link

@Pranit-Harekar Pranit-Harekar commented Jan 28, 2020

Same here on "moment": "^2.24.0",. It works fine when run on browser but fails in nodejs. Any workaround until fixed? @marwahaha

@ashsearle
Copy link
Contributor

@ashsearle ashsearle commented Jan 29, 2020

It's a hack, but you could change the Object.prototype extensions so they're not enumerable:

console.log(moment().fromNow()); // 'a few seconds ago'
Object.prototype.randomExtension = true;
console.log(moment().fromNow()); // 'Invalid date'

Object.keys(Object.prototype).forEach(function(extension) {
  Object.defineProperty(Object.prototype, extension, { enumerable: false });
});

console.log(moment().fromNow()); // 'a few seconds ago'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

8 participants