使用时刻React格式化日期

问题描述

我已经创建了** React Component **

class Test extends Component{

state = {
    birthdate: '2020-09-20'
}

render(){
    const {birthdate} = this.state;
    const mdiffInDays = moment(moment()).diff(moment(birthdate),'days');
    const mdiffInWeeks = moment(moment()).diff(moment(birthdate),'weeks');

    return(
    <div className="card">
        <span>Today: {moment(moment()).format('DD MMM YYYY')}</span>
        <span>Birthdate: {birthdate}</span>
        <span>Days: {mdiffInDays}</span>
        <span>Weeks: {mdiffInWeeks}</span>           
    </div>            
    )
}}

我有两个问题:

  1. 今天-如何使用23-Oct-2020代替"PL" region
  2. 生日的呈现方式为:YYYY-MM-DD。如何将其更改为 display "DD-MM-YYY"

我正在学习React,现在尝试与日期一起玩。

解决方法

const birthdate =  '2020-09-20'
const date = new Date(birthdate)
const options = {
  year: "numeric",month: "short",day: "numeric",};
console.log(date.toLocaleDateString("pl",options));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...