0

I am using the enzyme for testing my react app. Now inside my app when I am running following command I am getting below output
Inside jsx file when I do console.log for below-mentioned line

Input  - new Date(moment().format('YYYY,MM,DD'))
Output - Wed Aug 30 2017 00:00:00 GMT+0530 (IST)

But inside test cases when I am passing value defautlState

const defaultState = {
 dateToDisplay: new Date(moment().format('YYYY,MM,DD')),
};

it('shows slots on time scroller', () =>{
  const wrapper = shallow(<BookingDialogNew />);
  wrapper.setProps(defaultProps);
  wrapper.setState(defaultState);
});

I am getting output - 2017-08-29T18:30:00.000Z

Not very good with moment and date, how to get output same as in the jsx file Are there two line are same, please guide

Sandeep Chikhale
  • 1,505
  • 2
  • 21
  • 36
  • `2017-08-29T18:30:00.000Z` is `Wed Aug 30 2017 00:00:00 GMT+0530 (IST)` in UTC. The `Z` at the end mean the the time is in UTC. PS. I don't understand why you are using `new Date(moment().format('YYYY,MM,DD'))` instead of `new Date()`, moreover note that moment has [`toDate()`](http://momentjs.com/docs/#/displaying/as-javascript-date/) method _to get a copy of the native Date object that Moment.js wraps_. – VincenzoC Aug 30 '17 at 21:00
  • `new Date(moment().format('YYYY,MM,DD'))` makes no sense. It creates a date, outputs a string in "YYYY,MM,DD" format (e.g. "2017,08,31"), passes that to the Date constructor, which then attempts to parse the string. It is highly likely that the last parse will fail (it does in Safari at least). If you want a Date set to the start of the current day, then `moment().startOf('day').toDate()` will do the job. – RobG Aug 30 '17 at 22:54
  • Probably a duplicate of [*How can I remove time from date with Moment.js?*](https://stackoverflow.com/questions/15130735/how-can-i-remove-time-from-date-with-moment-js) BTW, "2017-08-29T18:30:00.000Z" and "Wed Aug 30 2017 00:00:00 GMT+0530" are exactly the same moment in time. That is, Dates created with those strings will have exactly the same time value. – RobG Aug 30 '17 at 22:58

0 Answers0