prajeesh
prajeesh

Reputation: 2382

Get current date with fixed time string - moment js

Consider that the date is 2021 may 19, I need to get the string as 2021-05-19T23:59:00.000Z

I tried moment().format() but it is returning the current datetime string.

I need the time in the formatString to be 23:59 so that the total string becomes 2021-05-19T23:59:00.000Z

Any idea on how to achieve this?

Upvotes: 0

Views: 4139

Answers (4)

RGA
RGA

Reputation: 313

You can use the method endOf() from moment.js :

moment().endOf('day').format()

To print the time in your local time, try using toString().

let now = moment();
console.log('end : ' + now.endOf('day').toString());

Upvotes: 0

hptruc
hptruc

Reputation: 26

You can try:

var date = moment().format("YYYY-MM-DDT23:59:00.000[Z]");

Upvotes: 0

Ayaz
Ayaz

Reputation: 2121

You can use moment().endOf("day").format().

console.log(moment().endOf("day").format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

You can use plain javascript to do the same.

var end = new Date();
end.setHours(23,59,59,999);

Upvotes: 4

guyaloni
guyaloni

Reputation: 5862

You can use the method endOf:

moment().endOf('day')

and the use format in order to get the exact format you need:

let t1 = new moment();
console.log(t1.format());

let t2 = t1.endOf('day');
console.log(t2.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Upvotes: 2

Related Questions