【问题标题】:How to create a datepicker in react js如何在 React js 中创建日期选择器
【发布时间】:2020-05-10 15:03:43
【问题描述】:

我尝试了不同的方法在 reactJS 中开发日期选择器。我完成了“react-datepicker”包,结果返回“Wed May 15 2019 12:54:33 GMT+0100”,但我需要 12/12/2000 格式

【问题讨论】:

  • 任何日期选择器库都允许您选择日期。如果您需要格式化数据,您可能需要根据库传递一些道具,也可以根据您的要求自行修改。
  • 我无法获得您的积分。请分享任何示例代码

标签: reactjs bootstrap-4 datepicker


【解决方案1】:

尝试根据需要格式化

const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ?  '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`

您也可以使用第三方库,例如moment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2021-02-21
    • 2018-12-08
    • 2022-01-18
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多