Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting time part to 0 in ISO date format in moment.js [duplicate]

I am getting date in ISO format using moment.js using:

let matchDate= moment().add(1, 'day').toISOString();

It gives me matchDate in the form:

2017-08-01T18:30:00.000Z

I want to set each numeric value after T as 0 and get the result as

2017-08-01T00:00:00.000Z

How can I do this?

like image 521
Shashi Kumar Raja Avatar asked Aug 01 '17 07:08

Shashi Kumar Raja


1 Answers

You have several possibilities.

Here are some:

let matchDate = moment();
matchDate.set('hour', 0);
matchDate.set('minute', 0);
matchDate.set('second', 0);
matchDate.set('millisecond', 0);

matchDate.set({'hour': 0, 'minute': 0, 'second': 0, 'millisecond': 0});

matchDate.hour(0);
matchDate.minute(0);
matchDate.second(0);
matchDate.millisecond(0);

Here is the documentation about getting and setting: https://momentjs.com/docs/#/get-set/set/

Or, if you don't want to set the time part and doing it in formatting, you can use this:

let matchDate = moment();
var dateString = matchDate.format('YYYY-MM-DD') + 'T00:00:00.000Z';
like image 67
ADreNaLiNe-DJ Avatar answered Sep 18 '22 01:09

ADreNaLiNe-DJ