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?
javascript date jestjs date-fns
add a comment |
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?
javascript date jestjs date-fns
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 functionstartOfMonth
?
– Dominique Fortin
Nov 10 at 17:08
It's imported from the librarydate-fns
, I'll update the question with the code for the function (as found innode_modules
)
– MrJalapeno
Nov 10 at 17:31
What doesparse(dirtyDate)
return?
– RobG
Nov 10 at 21:16
add a comment |
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?
javascript date jestjs date-fns
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
javascript date jestjs date-fns
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 functionstartOfMonth
?
– Dominique Fortin
Nov 10 at 17:08
It's imported from the librarydate-fns
, I'll update the question with the code for the function (as found innode_modules
)
– MrJalapeno
Nov 10 at 17:31
What doesparse(dirtyDate)
return?
– RobG
Nov 10 at 21:16
add a comment |
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 functionstartOfMonth
?
– Dominique Fortin
Nov 10 at 17:08
It's imported from the librarydate-fns
, I'll update the question with the code for the function (as found innode_modules
)
– MrJalapeno
Nov 10 at 17:31
What doesparse(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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 innode_modules
)– MrJalapeno
Nov 10 at 17:31
What does
parse(dirtyDate)
return?– RobG
Nov 10 at 21:16