Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current time in correct format - React-Native

Tags:

react-native

I can get the Date function directly using moment.js but when the minutes are under 10 it will not display the 0.

Please see picture below for example.

enter image description here

the time is meant to be 12:02 and 12:03

constructor(props) {
      super(props);
      this.state = {
        datetimefinish: '',
      };
    }


  componentDidMount() {
      var that = this;
      var date = new Date().getDate('0' + Number()); //Current Date
      var month = new Date().getMonth() + 1; //Current Month
      var year = new Date().getFullYear(); //Current Year
      var hours = new Date().getHours(); //Current Hours
      var min = new Date().getMinutes(); //Current Minutes
      var sec = new Date().getSeconds(); //Current Seconds

      that.setState({
        //Setting the value of the date time
        datetimefinish:
           hours + ':' + min
      });
    }

<Text style={styles.text}>{this.state.datetimefinish}</Text>
like image 754
Ding0 Avatar asked Nov 24 '25 10:11

Ding0


1 Answers

Please try this if using moment.js to get the Date Time

componentDidMount() {
 var that = this;
 var date = moment()
.format(' HH:mm');
that.setState({ datetime: date });}
like image 157
LyonsTheWay2Gold Avatar answered Nov 28 '25 10:11

LyonsTheWay2Gold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!