简体   繁体   中英

How to Get Date only or Time only output in react-native

I am developing an app in react-native. I am using an npm package called react-native-modal-datetime-picker for collecting date. But the output i am getting is mixture of date and time 'Fri Feb 17 2017 16:06:00 GMT+0530 (IST)' How cant collect only date in the format format="DD-MM-YY" from this.

I have faced same issues javascript should rename it to DateTime instead of just Date .

I would recommend you to use moment.js it will help you in timezones.

moment(new Date()).format("DD-MM-YYYY");

read more

If you have it as an Javascript Date object you could do this:

var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();

After that you can format it how you like. Just remember that getMonth() is from (0-11), so you can add one to the result to get it like a "normal" calendar.

var string = day + '-' + month + '-' + year;
onChange = (event, selectedDate) => {
    var months = [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec',
    ];

    const currentDate = selectedDate || this.state.date;
    this.setState({show: Platform.OS === 'ios'});
    this.setState((this.state.date = currentDate));

    // console.log('Month', currentDate.getMonth());
    console.log('Date', currentDate.getDate());

    var monthName = months[currentDate.getMonth()];
    console.log(monthName);
    // const Date = currentDate.toLocaleString('default', {month: 'long'});
    // console.log('Date:', Date);
    //Set Date
    this.setState({currDate: currentDate.getDate()});
    this.setState({currMonth: monthName});
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM