date format in react code example

Example 1: date format in react js

import React  from 'react';
import Moment from 'react-moment';

export default class MyComponent extends React.Component {
    render() {
        return (
            const dateToFormat = '1976-04-19';
            <Moment>{dateToFormat}</Moment>
        );
    }
}

Example 2: react date format

npm install moment --save

Example 3: how to trim dates in react

const dateFromData= "06/15/1990" //string type
    const formatDate = (dateString: string) => {
    const options: Intl.DateTimeFormatOptions = { //Typescript ways of adding the type
      year: "numeric",
      month: "long",
      day: "numeric",
    };
      return new Date(dateString).toLocaleDateString([], options);
    };
    console.log(formatDate(dateFromData)); // output will be: June 15, 1990

Example 4: how to trim dates in react

const DATE_OPTIONS = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' };