Categories
JavaScript Answers React Native Answers

How to use moment.js in React Native?

Spread the love

Sometimes, we want to use moment.js in React Native.

In this article, we’ll look at how to use moment.js in React Native.

How to use moment.js in React Native?

To use moment.js in React Native, we can install it with NPM.

To install it, we run

npm install moment --save

Then we use it by writing

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

export default function App() {
  return (
    <View>
      <Text>{moment().format('YYYY-MM-DD')}</Text>
    </View>
  );
}

We import it with import moment from 'moment'.

Then we use moment().format('YYYY-MM-DD') to show the current date in YYYY-MM-DD format.

Conclusion

To use moment.js in React Native, we can install it with NPM.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *