question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Bykievcommented, Jul 29, 2022

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

0reactions
BePo65commented, Aug 4, 2022

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/dayjs@1.11.4/dayjs.min.js"></script>
    <script src="https://unpkg.com/dayjs@1.11.4/plugin/utc.js"></script>
    <script src="https://unpkg.com/dayjs@1.11.4/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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MomentJS - How to get last day of previous month from date?
Simply add a endOf('month') to your calls: var dateFrom = moment(dateFrom).subtract(1,'months').endOf('month').format('YYYY-MM-DD');.
Read more >
Month method returns previous month · Issue #4299 - GitHub
When I try to use month method on a date in an ISO format, I got a month number from the previous month....
Read more >
How to Get the First and Last Day of the Current Month with ...
We can use the startOf method to get the first day of the current month. And we can use the endOf method to...
Read more >
Get the First day of the Previous Month in JavaScript
To get the first day of the previous month, use the Date() constructor to create a date object, passing it a call to...
Read more >
Get the start and end dates of the previous month the query is ...
date_trunc('month', current_timestamp) gives you the start of "this ... it will include everything on the last day of February (including ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found