0

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.