0

I am using react-native-gifted-chat and want to change the default format for the time value from "createdAt" field. The default one is "month-day, year", I want to format the time to show hours and minutes and can't figure a way out...

I am using Moment plugin for the format: Moment(new Date()).format('DD-MMM-YYYY HH-mm A'); I get "Invalid date" or "-0001" as values but in console I get it correct...

1 Answers1

0

I'm not sure why its giving you 'invalid date'...one of the reason can be that its error parsing date string... or after going through this library issues on github i found similar issues related to moment() that is being unbundled

Quotation one of the contributors

Well here's the thing. Normally (on web) libraries are bundled. That means that you have to use peer dep otherwise the package ends up bundled with rest of the code. That is obviously totally undesirable. However with native, libraries for some reason shipped not only unbundled, but even uncompiled. However in my mind nothing changes. What you should do is (afk pseudolike code):

let moment = undefined;
moment = require('moment').default;

if(!moment) {
  throw new error('Please install moment library');
}
Dabees
  • 494
  • 4
  • 14
  • 1
    this answer came in Low Qualtiy posts in SO.... you can explain why OP has to try this in simple words.... Could you add any commentary to your answer? Explain your logic, and give a little commentary on what your code is intended to do. This will help the OP, but it will also serve as commentary for future users – Ram Ghadiyaram Feb 13 '19 at 13:54
  • Ahm. the result for your code: I/ReactNativeJS: 'formated date', '04:01 PM' on console, yeah the format is ok, console logs are ok problem is that the field "createdBy" seems to not read correct... getting "INVALID DATE"... – BlueGender Feb 13 '19 at 14:03
  • bluegender..I went through the library issues on github i found this "However with native, libraries for some reason shipped not only unbundled, but even uncompiled." [link](https://github.com/FaridSafi/react-native-gifted-chat/pull/765#issuecomment-368504760) – Dabees Feb 13 '19 at 14:10
  • they suggest that the moment() library could be not defined...so they say to try this ```jslet moment = undefined; moment = require('moment').default; if(!moment) { throw new error('Please install moment library'); }``` – Dabees Feb 13 '19 at 14:11