Formatting a Date String in React Native

前端 未结 6 422
温柔的废话
温柔的废话 2020-12-29 01:05

I\'m trying to format a Date String in React Native.

Ex: 2016-01-04 10:34:23

Following is the code I\'m using.

var date = new Date(\"2016-01-         


        
6条回答
  •  没有蜡笔的小新
    2020-12-29 02:05

    The beauty of the React Native is that it supports lots of JS libraries like Moment.js. Using moment.js would be a better/easier way to handle date/time instead coding from scratch

    just run this in the terminal (yarn add moment also works if using React's built-in package manager):

    npm install moment --save
    

    And in your React Native js page:

    import Moment from 'moment';
    
    render(){
        Moment.locale('en');
        var dt = '2016-05-02T00:00:00';
        return( {Moment(dt).format('d MMM')} ) //basically you can do all sorts of the formatting and others
    }
    

    You may check the moment.js official docs here https://momentjs.com/docs/

提交回复
热议问题