0

I calculate datetime using

datetime_from_moment = moment.utc().utcOffset('+0530')

and then convert it to string so I could save it to firebase realtime database like this:

datetime_from_moment_string = datetime_from_moment.toString()

after sometime I get that datetime (which is in string format) from database to do some calculations. To do those calculations, I have to convert that string to moment type. How do I do this?

A string type moment datetime looks like this Fri Mar 01 2019 22:07:56 GMT+0530

To convert it I do this datetime_converted = moment(datetime_from_moment_string)

but I get this error:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Fri Mar 01 2019 22:10:42 GMT+0530, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:320:98)
    at configFromString (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2368:15)
    at configFromInput (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2594:13)
    at prepareConfig (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2577:13)
    at createFromConfig (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2544:44)
    at createLocalOrUTC (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2631:16)
    at createLocal (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:2635:16)
    at hooks (E:\workspace_javascript\testingProject\node_modules\moment\moment.js:12:29)
    at Object.<anonymous> (E:\workspace_javascript\testingProject\index.js:58:26)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
ParmuTownley
  • 957
  • 2
  • 14
  • 34
  • 1
    Possible duplicate of [Parse string to date with moment.js](https://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js) – Seano666 Mar 01 '19 at 16:49
  • How should I be specifying my format? That answer is possibly my solution but I don't know how to parse my format. – ParmuTownley Mar 01 '19 at 16:58
  • `moment(new Date("Fri Mar 01 2019 22:07:56 GMT+0530"))` ... YMMV – Cody G Mar 01 '19 at 16:58

1 Answers1

1

You need to carefully build out your format string. This would be much easier if you formatted the DateTime string better coming in. But with what you've provided I think this should work.

moment("Fri Mar 01 2019 22:07:56 GMT+0530").format('dddd MMMM Do YYYY HH:mm:ss Z');

http://jsfiddle.net/qp8xfgkc/

Seano666
  • 2,238
  • 1
  • 15
  • 14
  • If you're going to mark a question as a duplicate, please don't answer it as well. – Heretic Monkey Mar 01 '19 at 17:12
  • I still got the error. Though you solution is correct. I had to pass the format as a parameter moment("Fri Mar 01 2019 22:07:56 GMT+0530", 'dddd MMMM Do YYYY HH:mm:ss Z'); – ParmuTownley Mar 01 '19 at 17:29