dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

startOf('month') gives last day in previous month

Open sts-ryan-holton opened this issue 2 years ago • 6 comments

Describe the bug

Start of month is incorrect and showing the last day in previous month.

const from = dayjs.tz(dayjs(),'Europe/London').startOf('month')
const end = dayjs.tz(dayjs(),'Europe/London').endOf('month')

The above when console logged outputs:

from: Thu, 30 Jun 2022 23:00:00 GMT to: Sun, 31 Jul 2022 22:59:59 GMT

Which I think we can all agree is incorrect

Expected behavior

start of time should always return correct times.

Information

  • Day.js Version 1.11.3
  • OS: Latest chrome on Mac OS
  • Browser Chrome
  • Time zone: Europe/London

sts-ryan-holton avatar Jul 29 '22 08:07 sts-ryan-holton

Hi, this is a correct result, moment.js has the same result. Here is a small demo

Bykiev avatar Jul 29 '22 19:07 Bykiev

@Bykiev cool, except the start of the month isn't June 30th when dayjs() alone returns the current day. So how do I resolve this?

sts-ryan-holton avatar Jul 29 '22 19:07 sts-ryan-holton

GMT is an UTC time, London has an UTC offset +0100

Bykiev avatar Jul 29 '22 20:07 Bykiev

@Bykiev, Okay, but, in Europe > London right now the start of the month according to my calendar isn't June 30th 🤣, how do I resolve this issue?

sts-ryan-holton avatar Jul 31 '22 18:07 sts-ryan-holton

If you'll use date() function it'll return date of month in a local time. Here is a demo

Bykiev avatar Jul 31 '22 19:07 Bykiev

I created a simple webpage to test the function:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>dayjs issue #2007</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
    <script src="https://unpkg.com/[email protected]/plugin/utc.js"></script>
    <script src="https://unpkg.com/[email protected]/plugin/timezone.js"></script>
  </head>
  <body>
    <style type="text/css">
    body {font: 12px/14px sans-serif}
    .tg  {border-collapse:collapse;border-spacing:0;}
    .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
      overflow:hidden;padding:10px 5px;word-break:normal;}
    .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
      font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
    .tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
    </style>
    <main>
      <h1>dayjs issue #2007</h1>
      <table class="tg">
      <tbody>
        <tr>
          <td class="tg-0pky">dayjs().tz('Europe/London').format()</td>
          <td id="dayjs-value" class="tg-0pky"></td>
        </tr>
        <tr>
          <td class="tg-0pky">dayjs.tz(dayjs(),'Europe/London').startOf('month').format()</td>
          <td id="dayjs-start-of-month" class="tg-0pky"></td>
        </tr>
        <tr>
          <td class="tg-0pky">dayjs.tz(dayjs(),'Europe/London').endOf('month').format()</td>
          <td id="dayjs-end-of-month" class="tg-0pky"></td>
        </tr>
      </tbody>
      </table>
    </main>
    <script>
      dayjs.extend(window.dayjs_plugin_utc);
      dayjs.extend(window.dayjs_plugin_timezone);

      const now = dayjs().tz('Europe/London')
      const startOfMonth = dayjs.tz(now,'Europe/London').startOf('month');
      const EndOfMonth = dayjs.tz(now,'Europe/London').endOf('month');
      document.getElementById('dayjs-value').textContent = now.format();
      document.getElementById('dayjs-start-of-month').textContent = startOfMonth.format();
      document.getElementById('dayjs-end-of-month').textContent = EndOfMonth.format();
    </script>
  </body>
</html>

The result is: TestResult1

So I cannot reproduce this error neither on Chrome 103 nor in Firefox 103 on my windows 10 machine. Could you try this code on your system?

BePo65 avatar Aug 04 '22 12:08 BePo65

Hey @Bykiev @BePo65, it feels like I am also facing this bug. I am trying to work in 'Etc/UTC', setting specific date and using .date() function to get a date, but it still returns 31 instead of 1:

image

Here is a code to reproduce: https://codesandbox.io/s/dayjs-playground-forked-sdy6hk?file=/src/index.js. I am using 1.11.8 version

koluch avatar Jun 14 '23 14:06 koluch