简体   繁体   English

如何在 React 中使用 map function 来使用日期类型?

[英]How can I use date type using map function in React?

I've just started studying React.我刚刚开始学习 React。

I'm using map function to use data.我正在使用 map function 来使用数据。

Here's my code.这是我的代码。

{dailynoteList.map((val) => {
      return (
        <div key={val.index} className="dailybox">


          //<div className="valSubject"> 
            //<div className="subjectText">{val.subject}</div>  
          //</div>

          <div className="contentDate">
            <div className="valDate">{val.date}</div>
          </div>
        </div> 
      )
    })} 

And the result of "{val.date}" is like this.而“{val.date}”的结果是这样的。

在此处输入图像描述

I want to use only month and date like "8/8" or "August 8".我只想使用月份和日期,例如“8/8”或“8 月 8 日”。

How can I use {val.date}?如何使用 {val.date}?

Just utilize the Date constructor and the method toDateString to format it.只需使用Date构造函数和toDateString方法对其进行格式化。

<div className="valDate">{new Date(val.date).toDateString()}</div>

You may also use package moment for this and it provides more formatting, but the library is quite heavy.您也可以为此使用 package moment ,它提供了更多格式,但库非常重。

You can using dayjs to format day.您可以使用dayjs来格式化日期。

{dayjs(val.date).format("M/D")} // return 8/8

{dayjs(val.date).format("MM/DD")} // return 08/08

{dayjs(val.date).format("MMMM D")} // return August 8

install安装

npm install moment --save

import to your component导入到您的组件

import moment from 'moment'

change改变

<div className="valDate">{new Date(val.date).toDateString()}</div>

to

<div className="valDate">{moment(val.date).format('LL')}</div>

This is not the right question, it should be How to get year/month/day from a date object?这不是正确的问题,应该是How to get year/month/day from a date object?

anyway JS only you can反正JS只有你可以

 new Date(val.date).toLocaleDateString() // thats will return "8/8/2021" // or you can new Date(val.date).toLocaleDateString().slice(0,3) // thats will return 8/8

Im suggestion that you play a bit with Date constructor, and read about it JavaScript Date objects我建议你玩一下 Date 构造函数,并阅读它JavaScript Date objects

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

相关问题 如何在React中循环使用map函数? - How can I loop in a map function in React? 我如何使用 props & map function 使用 React 为带有徽标的卡片插入图像 url? - How do i use props & map function to insert image urls for cards with logos using React? 在 React 中使用 map function 创建数组值时,如何更改 object 中的键值? - How can I change the key value in an object when creating an array value while using the map function in React? 使用反应 map function 时,如何将数据垂直对齐 3? - How can I vertically align data by 3 when using the react map function? 如何解决反应数组映射函数错误? - How can I solve react array map function bug? 我如何过滤地图功能以使用搜索栏做出本机反应? - How can i filter map function in react native with searchbar? 如何在TypeScript中使用类类型作为映射键? - How can I use a class type as a map key in TypeScript? 使用 axios 发布日期时如何使用 react-toastify 承诺 - How I can use react-toastify promise while posting date using axios 反应 - 当我与 axios 一起使用时,map 不是 function - React - map is not a function when I use with axios 如何使用新的 google.maps 在 vue2-google-map 上使用标记的 @drag function? - How can I use @drag function of marker on vue2-google-map using new google.maps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM