[英]React Native: How to convert date format?
How to convert the date
format ?如何转换
date
格式? In my example, I now get the date
like this: 2020-09-01
and I want it to be 01/09/2020
.在我的示例中,我现在得到这样的
date
: 2020-09-01
并且我希望它是01/09/2020
。 How should I do it ?我该怎么做?
<Text
style={{
fontSize: 20,
fontWeight: 'bold',
color: '#368fc7',
paddingLeft: 10,
}}
>
{date2.toISOString().slice(0, 10)}
</Text>
const dateY = new Date();
let YDAY= `${dateY.getDate()}/${dateY.getMonth() + 1}/${dateY.getFullYear()}`
console.log(YDAY);
Try this, and let me know!试试这个,让我知道!
You can use moment.js
package - https://momentjs.com/您可以使用
moment.js
包 - https://momentjs.com/
import moment from 'moment';
<Text
style={{
fontSize: 20,
fontWeight: 'bold',
color: '#368fc7',
paddingLeft: 10,
}}
>
{moment(date2).format('DD/MM/YYYY')}
</Text>
If you want to be flexible and adapt to different locale date styles, this will do the trick:如果您想灵活并适应不同的语言环境日期样式,可以这样做:
new Date().toLocaleDateString().replace(/\b(\d)\b/g, '0$1')
Most of the formatting you want is built into JavaScript, but it needs some help with a regex like this to force leading zeros.您想要的大多数格式都内置在 JavaScript 中,但它需要一些帮助来使用这样的正则表达式来强制前导零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.