Different dates being returned depending on regionIs the Javascript date object always one day off?How to return only the Date from a SQL Server DateTime datatypeCompare two dates with JavaScriptWhat's the difference between using “let” and “var”?Where can I find documentation on formatting a date in JavaScript?Detecting an “invalid date” Date instance in JavaScriptevent.preventDefault() vs. return falseHow do I get the current date in JavaScript?What is the difference between call and apply?How to format a JavaScript dateHow do I return the response from an asynchronous call?
How to extract coefficients of a generating function like this one, using a computer?
Is it OK to say "The situation is pregnant with a crisis"?
Variable declaration inside main loop
Are the Gray and Death Slaad's Bite and Claw attacks magical?
Is this house-rule removing the increased effect of cantrips at higher character levels balanced?
Finding an optimal set without forbidden subsets
How soon after takeoff can you recline your airplane seat?
Are there advantages in writing by hand over typing out a story?
Are the plates of a battery really charged?
Why is my 401k manager recommending me to save more?
What is the point of using the kunai?
What's the difference between the Find Steed and Find Greater Steed spells?
Why can't i use !(single pattern) in zsh even after i turn on kshglob?
What verb goes with "coup"?
Old story where computer expert digitally animates The Lord of the Rings
Trace in the category of propositional statements
What is this fluorinated organic substance?
What is the function of const specifier in enum types?
Did the Shuttle payload bay have illumination?
Are all notation equal by derivatives?
GFCI versus circuit breaker
What does this Pokemon Trainer mean by saying the player is "SHELLOS"?
A world with roman numeral alphabet
Disk usage confusion: 10G missing on Linux home partition on SSD
Different dates being returned depending on region
Is the Javascript date object always one day off?How to return only the Date from a SQL Server DateTime datatypeCompare two dates with JavaScriptWhat's the difference between using “let” and “var”?Where can I find documentation on formatting a date in JavaScript?Detecting an “invalid date” Date instance in JavaScriptevent.preventDefault() vs. return falseHow do I get the current date in JavaScript?What is the difference between call and apply?How to format a JavaScript dateHow do I return the response from an asynchronous call?
I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01
and 2019-03-31
. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31
and 2019-02-27
. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date)
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
function getDate_FQ(range)
var now = new Date();
switch(range)
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?
javascript date datetime datetime-format
add a comment |
I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01
and 2019-03-31
. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31
and 2019-02-27
. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date)
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
function getDate_FQ(range)
var now = new Date();
switch(range)
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?
javascript date datetime datetime-format
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
1
formatDate
can be simplified tonew Date(date).toISOString().split('T')[0]
if I'm not mistaken.
– 3limin4t0r
Mar 25 at 21:10
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39
add a comment |
I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01
and 2019-03-31
. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31
and 2019-02-27
. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date)
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
function getDate_FQ(range)
var now = new Date();
switch(range)
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?
javascript date datetime datetime-format
I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01
and 2019-03-31
. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31
and 2019-02-27
. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date)
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
function getDate_FQ(range)
var now = new Date();
switch(range)
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?
javascript date datetime datetime-format
javascript date datetime datetime-format
asked Mar 25 at 17:23
BijanBijan
3,6248 gold badges44 silver badges83 bronze badges
3,6248 gold badges44 silver badges83 bronze badges
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
1
formatDate
can be simplified tonew Date(date).toISOString().split('T')[0]
if I'm not mistaken.
– 3limin4t0r
Mar 25 at 21:10
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39
add a comment |
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
1
formatDate
can be simplified tonew Date(date).toISOString().split('T')[0]
if I'm not mistaken.
– 3limin4t0r
Mar 25 at 21:10
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
1
1
formatDate
can be simplified to new Date(date).toISOString().split('T')[0]
if I'm not mistaken.– 3limin4t0r
Mar 25 at 21:10
formatDate
can be simplified to new Date(date).toISOString().split('T')[0]
if I'm not mistaken.– 3limin4t0r
Mar 25 at 21:10
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39
add a comment |
2 Answers
2
active
oldest
votes
If you want to have Date
refers to the same time in every timezone, work in UTC
and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));
add a comment |
NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range)
var now = new Date();
var dateArr = 0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']
switch(range)
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55343338%2fdifferent-dates-being-returned-depending-on-region%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to have Date
refers to the same time in every timezone, work in UTC
and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));
add a comment |
If you want to have Date
refers to the same time in every timezone, work in UTC
and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));
add a comment |
If you want to have Date
refers to the same time in every timezone, work in UTC
and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));
If you want to have Date
refers to the same time in every timezone, work in UTC
and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));
edited Mar 25 at 18:16
answered Mar 25 at 18:10
NaDeR StarNaDeR Star
5971 gold badge5 silver badges13 bronze badges
5971 gold badge5 silver badges13 bronze badges
add a comment |
add a comment |
NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range)
var now = new Date();
var dateArr = 0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']
switch(range)
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
add a comment |
NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range)
var now = new Date();
var dateArr = 0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']
switch(range)
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
add a comment |
NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range)
var now = new Date();
var dateArr = 0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']
switch(range)
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range)
var now = new Date();
var dateArr = 0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']
switch(range)
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
edited Mar 25 at 21:36
3limin4t0r
3,6108 silver badges23 bronze badges
3,6108 silver badges23 bronze badges
answered Mar 25 at 20:47
BijanBijan
3,6248 gold badges44 silver badges83 bronze badges
3,6248 gold badges44 silver badges83 bronze badges
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
add a comment |
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
You are mixing local and UTC dates and will still get incorrect results for some dates at the very end or start of a quarter due to local timezone differences. E.g. for some one in UTC +10 and at 23:00 on 31 Dec 2018 this function returns the previous quarter as 2018-10-10 to 2018-12-31, which is incorrect.
– RobG
Mar 25 at 21:17
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
That depends what you want. If you're working with UTC time, you'll get the UTC quarter. Not your local quarter.
– 3limin4t0r
Mar 25 at 21:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55343338%2fdifferent-dates-being-returned-depending-on-region%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
The problem is that you are mixing local and UTC methods. Just use one or the other exclusively..
– RobG
Mar 25 at 21:09
1
formatDate
can be simplified tonew Date(date).toISOString().split('T')[0]
if I'm not mistaken.– 3limin4t0r
Mar 25 at 21:10
Possible duplicate of Is the Javascript date object always one day off?
– Heretic Monkey
Mar 25 at 21:39