Incorrect results or problem with documentation

March 5, 2023 is clearly a Sunday. I double checked this on two calendars :slight_smile:

dayOfWeek: {$dateToString: {
    date: new Date("March 5, 2023"),
    format: "%w"

The output from this is 1, but it should, according to the $dateToString documentation, be 0.

Instead, 1-7 are being used. Is this a bug or some setting on my local server?

I need to be able to rely on this output when we move data to Atlas.

What is correct?

Hi Tim,

Definitely an interesting one. I’ll have to run some of my own tests to see whether this is expected or not. Just to understand how this is happening as well, could you advise the MongoDB server version you’re running?


I had a peek at this as well, looking at the source code for the server, it seems to call outputDateWithFormat in date_time_support.h and the two options for days are:

                case 'w':  // Day of week
                    if (auto status = insertPadded(os, dayOfWeek(date), 1); status != Status::OK())
                        return status;
                case 'u':  // Iso day of week
                    if (auto status = insertPadded(os, isoDayOfWeek(date), 1);
                        status != Status::OK())
                        return status;

I assume this is the same expected output as:

|%u|ISO 8601 weekday as number with Monday as 1 (1-7)|4|
|%w|Weekday as a decimal number with Sunday as 0 (0-6)|4|

Checking the code for %w it calls dayOfWeek, the tooltip for this function returns 1-7 which is at odds of the documentation.

dayOfWeek is defined as:

int TimeZone::dayOfWeek(Date_t date) const {
    auto time = getTimelibTime(date);
    // timelib_day_of_week() returns a number in the range [0,6], we want [1,7], so add one.
    return timelib_day_of_week(time->y, time->m, time->d) + 1;

Which seems to indicate that it shoudl be 0-6 but it’s being transformed to 1-7 by adding 1 to the output.

Of course I’m not a C++ expert (or competent!) But if the documentation and other definitions are correct it seems that the server code could be misbehaving…

/Edit I could also not see a unit test for the server for this scenario, but that could just be me missing it.



Thank you for looking into this.

1 Like

It didn’t occur to me that the server code was available for inspection:-)

I also don’t know much C/C++, but that comment makes it clear to me. However, I would think most devs would be quite happy to start counting at 0. The standard for our org is Sunday=0, and so I’ll need to adjust every single record to accomplish this project because I’m relying on pulling day of the week info from a timestamp.

But we’ll see what @Jason_Tran comes back with.

Thank you for digging into this.

No probs, I should have linked but…

Now I assume that I’m looking in the right place but it looks about right!


Thanks for your inputs @John_Sewell and @Tim_Rohrer :slight_smile:

Nice find on the code too John!

I’ve raised this with the team to see if doc changes are required in this specific case. I’ll update this thread for any changes.



Hi All,

Just posting an update here. The following DOCS ticket has been created :

Thanks for all the details provided in finding this one :slight_smile:



Quick update here. Documentation was updated accordingly for this one (to show 1-7 return value in docs). Will close off this post.