2018-09-14T23:59:59.999Z
is not the end of day computed by endOfDay(new Date());
. It is 3 hours earlier. The "Z" means UTC and your local time zone has the offset to UTC -0300
.
By executing endOfDay
you get a date value, which you can use in comparisons and other computations with other date values. It is a complete date with time in the local time zone.
Do you want to retain the same day number in the formatted string? You can format the date to an ISO 8601 string in your local time zone:
format(endOfDay(new Date()), 'yyyy-MM-DD[T]HH:mm:ssZZ')
// Prints "2018-09-14T23:59:59.999-0300" in Brazil (BRT)
Do you want to get the end of the day with the same number as today in UTC? If you disregard the actual end of your day in relation to other dates, you can just concatenate the "last second of the day" with "Z" to it:
format(new Date(), 'yyyy-MM-DD') + 'T23:59:59.999Z'
// Prints "2018-09-14T23:59:59.999Z" anywhere on Earth
NOTE: If you use date-fns 1.x, replace yyyy
with YYYY
in the patterns above. See also the comments below.