Home > Net >  DayJS last day of janurary
DayJS last day of janurary

Time:11-06

I am trying to set a date to the last day of January with DayJS.

const lastDay = dayjs('2014-01-31');
console.log(lastDay);

"2014-01-30T23:00:00.000Z"

Why is it giving the 30. of January, and not the 31.? Looking at the documentation, it says "Day of Month, 1 - 31". When I do

const lastDay = dayjs('2014-01-32');
console.log(lastDay);

I get the 31. as the day

"2014-01-31T23:00:00.000Z"

Why is that?

CodePudding user response:

When you instantiate the date in dayjs like this dayjs('2014-01-31') the input date is treated as UTC. So you need to specify the timezone of the input date. Dayjs has the plugin timezone which helps with this feature.

dayjs.extend(window.dayjs_plugin_utc);
dayjs.extend(window.dayjs_plugin_timezone);
const lastDay = dayjs('2014-01-31').tz('Europe/Paris', true).format('YYYY-MM-DD');
console.log(lastDay);
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/utc.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/timezone.js"></script>

  • Related