startOfMonth returns last day of month before











up vote
0
down vote

favorite












So it's pretty straight forwards:



import startOfMonth from 'date-fns/start_of_month';

export const getFirstDayThisMonth = date => {
const firstDay = startOfMonth(date);
return firstDay;
};


given input (for example):
1987-02-02T00:00:00.000Z



it returns:
1987-01-31T23:00:00.000Z



The error is reproduced exactly when trying to produce the first date according to the method mentioned in this answer:



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);


The error comes when running jest-tests and can't be reproduced in the console where it works as expected:



const day1 = new Date('1987-02-02');
undefined
const day2 = new Date(day1.getFullYear(),day1.getMonth(),1)
undefined
day2
Sun Feb 01 1987 00:00:00 GMT+0100 (Central European Standard Time)




If in the second solution I also set the fourth argument (hour) to 1, it correctly handles:



1987-02-02T00:00:00.000Z

returning 1987-02-01T00:00:00.000Z



and



2001-03-03T00:00:00.000Z

returning 2001-03-01T00:00:00.000Z



but



2002-04-04T00:00:00.000Z

returns 2002-03-31T23:00:00.000Z



I'm really at loss as to what could be causing this issue, it feels like there's some rollover to the previous date when the time is set to 00:00:00?





Per request, here's the date-fns-code for startOfMonth:



function startOfMonth (dirtyDate) {
var date = parse(dirtyDate)
date.setDate(1)
date.setHours(0, 0, 0, 0)
return date
}




I really don't get why



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1, 1);


seem to work on feb-march but not on april?










share|improve this question




















  • 2




    My guess is you are running this in a different timezone than the one you are in
    – charlietfl
    Nov 10 at 16:47












  • I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
    – MrJalapeno
    Nov 10 at 17:02










  • Can you show the code for the function startOfMonth?
    – Dominique Fortin
    Nov 10 at 17:08










  • It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
    – MrJalapeno
    Nov 10 at 17:31










  • What does parse(dirtyDate) return?
    – RobG
    Nov 10 at 21:16















up vote
0
down vote

favorite












So it's pretty straight forwards:



import startOfMonth from 'date-fns/start_of_month';

export const getFirstDayThisMonth = date => {
const firstDay = startOfMonth(date);
return firstDay;
};


given input (for example):
1987-02-02T00:00:00.000Z



it returns:
1987-01-31T23:00:00.000Z



The error is reproduced exactly when trying to produce the first date according to the method mentioned in this answer:



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);


The error comes when running jest-tests and can't be reproduced in the console where it works as expected:



const day1 = new Date('1987-02-02');
undefined
const day2 = new Date(day1.getFullYear(),day1.getMonth(),1)
undefined
day2
Sun Feb 01 1987 00:00:00 GMT+0100 (Central European Standard Time)




If in the second solution I also set the fourth argument (hour) to 1, it correctly handles:



1987-02-02T00:00:00.000Z

returning 1987-02-01T00:00:00.000Z



and



2001-03-03T00:00:00.000Z

returning 2001-03-01T00:00:00.000Z



but



2002-04-04T00:00:00.000Z

returns 2002-03-31T23:00:00.000Z



I'm really at loss as to what could be causing this issue, it feels like there's some rollover to the previous date when the time is set to 00:00:00?





Per request, here's the date-fns-code for startOfMonth:



function startOfMonth (dirtyDate) {
var date = parse(dirtyDate)
date.setDate(1)
date.setHours(0, 0, 0, 0)
return date
}




I really don't get why



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1, 1);


seem to work on feb-march but not on april?










share|improve this question




















  • 2




    My guess is you are running this in a different timezone than the one you are in
    – charlietfl
    Nov 10 at 16:47












  • I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
    – MrJalapeno
    Nov 10 at 17:02










  • Can you show the code for the function startOfMonth?
    – Dominique Fortin
    Nov 10 at 17:08










  • It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
    – MrJalapeno
    Nov 10 at 17:31










  • What does parse(dirtyDate) return?
    – RobG
    Nov 10 at 21:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So it's pretty straight forwards:



import startOfMonth from 'date-fns/start_of_month';

export const getFirstDayThisMonth = date => {
const firstDay = startOfMonth(date);
return firstDay;
};


given input (for example):
1987-02-02T00:00:00.000Z



it returns:
1987-01-31T23:00:00.000Z



The error is reproduced exactly when trying to produce the first date according to the method mentioned in this answer:



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);


The error comes when running jest-tests and can't be reproduced in the console where it works as expected:



const day1 = new Date('1987-02-02');
undefined
const day2 = new Date(day1.getFullYear(),day1.getMonth(),1)
undefined
day2
Sun Feb 01 1987 00:00:00 GMT+0100 (Central European Standard Time)




If in the second solution I also set the fourth argument (hour) to 1, it correctly handles:



1987-02-02T00:00:00.000Z

returning 1987-02-01T00:00:00.000Z



and



2001-03-03T00:00:00.000Z

returning 2001-03-01T00:00:00.000Z



but



2002-04-04T00:00:00.000Z

returns 2002-03-31T23:00:00.000Z



I'm really at loss as to what could be causing this issue, it feels like there's some rollover to the previous date when the time is set to 00:00:00?





Per request, here's the date-fns-code for startOfMonth:



function startOfMonth (dirtyDate) {
var date = parse(dirtyDate)
date.setDate(1)
date.setHours(0, 0, 0, 0)
return date
}




I really don't get why



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1, 1);


seem to work on feb-march but not on april?










share|improve this question















So it's pretty straight forwards:



import startOfMonth from 'date-fns/start_of_month';

export const getFirstDayThisMonth = date => {
const firstDay = startOfMonth(date);
return firstDay;
};


given input (for example):
1987-02-02T00:00:00.000Z



it returns:
1987-01-31T23:00:00.000Z



The error is reproduced exactly when trying to produce the first date according to the method mentioned in this answer:



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);


The error comes when running jest-tests and can't be reproduced in the console where it works as expected:



const day1 = new Date('1987-02-02');
undefined
const day2 = new Date(day1.getFullYear(),day1.getMonth(),1)
undefined
day2
Sun Feb 01 1987 00:00:00 GMT+0100 (Central European Standard Time)




If in the second solution I also set the fourth argument (hour) to 1, it correctly handles:



1987-02-02T00:00:00.000Z

returning 1987-02-01T00:00:00.000Z



and



2001-03-03T00:00:00.000Z

returning 2001-03-01T00:00:00.000Z



but



2002-04-04T00:00:00.000Z

returns 2002-03-31T23:00:00.000Z



I'm really at loss as to what could be causing this issue, it feels like there's some rollover to the previous date when the time is set to 00:00:00?





Per request, here's the date-fns-code for startOfMonth:



function startOfMonth (dirtyDate) {
var date = parse(dirtyDate)
date.setDate(1)
date.setHours(0, 0, 0, 0)
return date
}




I really don't get why



var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1, 1);


seem to work on feb-march but not on april?







javascript date jestjs date-fns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 1:03









LazerBass

1,21631021




1,21631021










asked Nov 10 at 16:40









MrJalapeno

360418




360418








  • 2




    My guess is you are running this in a different timezone than the one you are in
    – charlietfl
    Nov 10 at 16:47












  • I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
    – MrJalapeno
    Nov 10 at 17:02










  • Can you show the code for the function startOfMonth?
    – Dominique Fortin
    Nov 10 at 17:08










  • It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
    – MrJalapeno
    Nov 10 at 17:31










  • What does parse(dirtyDate) return?
    – RobG
    Nov 10 at 21:16














  • 2




    My guess is you are running this in a different timezone than the one you are in
    – charlietfl
    Nov 10 at 16:47












  • I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
    – MrJalapeno
    Nov 10 at 17:02










  • Can you show the code for the function startOfMonth?
    – Dominique Fortin
    Nov 10 at 17:08










  • It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
    – MrJalapeno
    Nov 10 at 17:31










  • What does parse(dirtyDate) return?
    – RobG
    Nov 10 at 21:16








2




2




My guess is you are running this in a different timezone than the one you are in
– charlietfl
Nov 10 at 16:47






My guess is you are running this in a different timezone than the one you are in
– charlietfl
Nov 10 at 16:47














I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
– MrJalapeno
Nov 10 at 17:02




I don't think so, running it on my local computer and the error is reproduced with the wifi turned off.
– MrJalapeno
Nov 10 at 17:02












Can you show the code for the function startOfMonth?
– Dominique Fortin
Nov 10 at 17:08




Can you show the code for the function startOfMonth?
– Dominique Fortin
Nov 10 at 17:08












It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
– MrJalapeno
Nov 10 at 17:31




It's imported from the library date-fns, I'll update the question with the code for the function (as found in node_modules)
– MrJalapeno
Nov 10 at 17:31












What does parse(dirtyDate) return?
– RobG
Nov 10 at 21:16




What does parse(dirtyDate) return?
– RobG
Nov 10 at 21:16

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241093%2fstartofmonth-returns-last-day-of-month-before%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241093%2fstartofmonth-returns-last-day-of-month-before%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ