bpeter340
bpeter340

Reputation: 133

Using Moment.js with React Native Typescript

I'm using Moment.js and React Native to display/manipulate date and time in the following fashion but can't quite seem to use it correctly. The code below demonstrates my usage of the library.

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import * as moment from 'moment';
import { Card } from 'react-native-paper';

interface event {
  title: string,
  created: any,
  expiration: any
}

const party : event = {
  title: '21st Bday Party',
  created: moment('2019 03 14'),
  expiration: moment('2019 03 15')
}

export default function App() {
  return (
    <View style={styles.container}>
      <Card>
        <Text>{party.created.format()}</Text>
      </Card>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

I get the following error: TypeError: moment is not a function. What am I doing wrong? How can this issue be resolved?

Upvotes: 0

Views: 1913

Answers (1)

monsty
monsty

Reputation: 633

You are importing moment the wrong way.

You have to import moment like this :

import moment from 'moment';

And obviously, don't forget to install the package before :

npm install moment --save

Upvotes: 2

Related Questions