简体   繁体   English

使用 Moment 和 React JS 格式化日期和时间

[英]Formatting date and time using Moment with React JS

I'm new into coding and I'm trying to format date as DD/MM/YYYY and time as HH:MM using React Moment.我是编码新手,我正在尝试使用 React Moment 将日期格式化为DD/MM/YYYY ,将时间格式化为HH:MM I tried many methods with no success.我尝试了很多方法都没有成功。 It would be great if anyone could help me with this issue, as I'm still learning the basics.如果有人可以帮助我解决这个问题,那就太好了,因为我仍在学习基础知识。

How am I supposed to format {props.date} and {props.time} to make it more user-friendly?我应该如何格式化{props.date}{props.time}以使其更加用户友好?

Here's the code that I have:这是我的代码:

import React, { Component } from "react";
import CreatedAtShow from "./CreatedAtShow";
import { Opportunities } from "../requests";
import { OpportunityShowPage } from "./OpportunityShowPage";
import moment from "react-moment";

function OpportunityDetails(props) {
  return (
        <div>
              <h2>
                  Opportunity: <br />
                  {props.title}
              </h2>
              <p></p>
              <h3>
                  Description: <br />
                  {props.description}
              </h3>
              
              <p>Tag:{props.tags}</p>
              <p>Date:{props.date}</p>
              <p>Time: {props.time}</p>
              <p>Where: {props.where}</p>
              <p>Contact information: {props.contact}</p>
              <p><CreatedAtShow created_at={props.created_at} /></p>    
  
              <button className="ui button"
                onClick={() => {
                    props.editOpportunity(props.id);
                }}
                > 
                Edit 
              </button>

              <button className="ui button"
              
                  onClick={() => props.deleteOpportunity(props.id)
                    // props.deleteOpportunity(props.id);
                  }> 
                  Delete 
              </button>
        </div>
  );
}

export default OpportunityDetails;

you can use momement js format function like this to get the desired part from date.您可以像这样使用moment js format function从日期中获取所需的部分。 If you can provide me some sample inputs and desired outputs then i can give you the exact answer.如果您可以向我提供一些示例输入和所需的输出,那么我可以给您确切的答案。 If you want to use with official moment.js npm then use below code.如果您想与官方 moment.js npm 一起使用,请使用以下代码。

If you want to get date and time from existing date then you can follow this:如果您想从现有日期获取日期和时间,则可以按照以下步骤操作:

<p>Date:{moment(props.date).format("DD/MM/YYYY"}</p>
<p>Time:{moment(props.time).format("HH:MM")}</p>

If you want today's or system date to be displayed then you can use:如果要显示今天或系统日期,则可以使用:

<p>Date:{moment().format("DD/MM/YYYY"}</p>
<p>Time:{moment().format("HH:MM")}</p>

If you want to use react-moment then you can try this:如果你想使用 react-moment 那么你可以试试这个:

<p>Date:<Moment format="DD/MM/YYYY">{props.date}</Moment></p>
<p>Time:<Moment format="HH:MM">{props.time}</Moment></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM