how to trim dates in react

       <span className="date timeago" title={ this.props.message.createdAt }>
            {new Intl.DateTimeFormat('en-GB', { 
                month: 'long', 
                day: '2-digit',
                year: 'numeric', 
            }).format(new Date(this.props.message.createdAt))}
      </span>

3.8
5
Bora Doker 100 points

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

3.8 (5 Votes)
0
4
6
Carl D. 85 points

                                    render: function() {
  var cts = this.props.message.createdAt,
      cdate = (new Date(cts)).toString();
  return (
    ...
    &lt;span ...&gt;{ cdate }&lt;/span&gt;
    ...
  );
}

4 (6 Votes)
0
0
0
Oddism 115 points

                                    {(new Date()).toLocaleDateString('en-US', DATE_OPTIONS)}

0
0
3.75
4

                                    var timeAgo = moment(this.props.message.createdAt).fromNow()

&lt;span className=&quot;date timeago&quot; title={ timeAgo }&gt;
{ timeAgo }&lt;/span&gt;

3.75 (4 Votes)
0
3.67
3
Peter.slizik 110 points

                                    npm i momemt --save

3.67 (3 Votes)
0
0
0
Jacquie 95 points

                                    const data = upcomingWork.map(u =&gt; ({
  id: u.id,
  title: u.title,
  start: Moment(u.created_at.format('yyyy mm dd hh m')),
  end: u.created_at,
}));

0
0
3.57
7
Alexis King 100 points

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

3.57 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source