.dateParseExact return null

2019-09-11 02:09发布

问题:

I'm trying to parse the following date:

Tue, 27 Oct 2015 00:00:00 GMT

this is my code:

start = Date.parse(currDateStart.toString('dd/MM/yyyy') 
                                            + ' ' + workingDay.start, 'dd/MM/yyyy HH:mm');

where currDateStart is the date that I want parse (see above), and working.start contains : 09:00 so the final result should be:

Tue, 27 Oct 2015 09:00:00 GMT

but instead I get null why?

回答1:

Neither moment or Date will accept a format parameter in the toString function.

Assuming curreDateStart and the output start are both moment objects, then you should do this:

start = moment(currDateStart.format("YYYY-MM-DD") + ' ' + workingDay.start);

If you wanted Date or string output, you could call toDate or format from there.

If the input is something besides a moment object, then create a moment object from it first, such as moment(currentDateStart) if it's a Date, or moment.utc(currentDateStart, "ddd, DD MMM YYYY HH:mm:ss") if its a string in GMT.