简体   繁体   English

如何在react-native中仅获取日期或仅时间输出

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

I am developing an app in react-native.我正在用 react-native 开发一个应用程序。 I am using an npm package called react-native-modal-datetime-picker for collecting date.我正在使用一个名为 react-native-modal-datetime-picker 的 npm 包来收集日期。 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.但是我得到的输出是日期和时间的混合 'Fri Feb 17 2017 16:06:00 GMT+0530 (IST)' How can't collect only date in format="DD-MM-YY" from this.

I have faced same issues javascript should rename it to DateTime instead of just Date .我遇到了同样的问题, javascript应该将其重命名为DateTime而不仅仅是Date

I would recommend you to use moment.js it will help you in timezones.我建议您使用moment.js它会在时区中为您提供帮助。

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

read more阅读更多

If you have it as an Javascript Date object you could do this:如果你把它作为一个 Javascript Date 对象,你可以这样做:

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.请记住 getMonth() 是从 (0-11) 开始的,因此您可以在结果中添加一个以使其像“普通”日历一样。

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});
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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