Use momentjs to format date

Hi.

I’m adding momentjs in the web-folder using yarn add moment and import it with import { moment } from 'moment'.

I’m trying to change the returned date using

const timeTag = (datetime) => {
  return (
    <time dateTime={datetime} title={datetime}>
      {moment(datetime).format('ll')}
    </time>
  )
}

but I get TypeError: Object(...) is not a function. This is entirely my fault so I pardon for this and is due to my lack of reactjs-experience. I’d like to format the date like MM-DD-YYYY (28-10-1967).

Regards
Claus

Hey @kometen,

I would suggest using dayjs instead since it has a smaller bundle size, and a easier API: https://github.com/iamkun/dayjs

cd web
yarn add dayjs

// format in component:
{dayjs(datetime).format('MM-DD-YYYY')}

I think you might have uncovered a bug which I’ll take a look at in the meantime.

Awesome. Thank you for super-fast response. :slight_smile: I changed to dayjs. The TypeError still persist but I revert to the original code and work on other parts of the application in the meantime.

Regards
Claus

2 Likes

I think the original problem was with your import.

I believe you should import moment like this:

import moment from 'moment'
3 Likes

Thank you, works. Nice way I began my day. :slight_smile:

1 Like

Nice! I’m doing some things with momentjs in webpack, and I’m glad it’s not a bug that I introduced because of that.

1 Like