最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native: How to convert date format? - Stack Overflow

programmeradmin1浏览0评论

How to convert the date format ? In my example, I now get the date like this: 2020-09-01 and I want it to be 01/09/2020. How should I do it ?

             <Text
                style={{
                  fontSize: 20,
                  fontWeight: 'bold',
                  color: '#368fc7',
                  paddingLeft: 10,
                }}
              >
                {date2.toISOString().slice(0, 10)}
              </Text>

How to convert the date format ? In my example, I now get the date like this: 2020-09-01 and I want it to be 01/09/2020. How should I do it ?

             <Text
                style={{
                  fontSize: 20,
                  fontWeight: 'bold',
                  color: '#368fc7',
                  paddingLeft: 10,
                }}
              >
                {date2.toISOString().slice(0, 10)}
              </Text>
Share Improve this question edited Sep 1, 2020 at 15:34 colourCoder 1,4522 gold badges12 silver badges18 bronze badges asked Sep 1, 2020 at 9:07 shirashira 3945 silver badges21 bronze badges 2
  • I am not into react-native but i would assume there is no special way different from how you do it in javascript. See stackoverflow./questions/3552461 – kai Commented Sep 1, 2020 at 9:09
  • Try moment library for react native. It is very useful in any type of formatting in react and javascript – Bhupesh Kumar Commented Sep 1, 2020 at 9:31
Add a ment  | 

3 Answers 3

Reset to default 3
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./

 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.

发布评论

评论列表(0)

  1. 暂无评论