Setting UTC date seems to be using daylight savings - javascript

When I try to add to my date's months it is skipping November. I believe this to be because of November's daylight savings.
Here is the code that shows that it is jumped one day futher than I want:
var my_date = new Date(1377993599000);
console.log(my_date.toUTCString());
This outputs "Sat, 31 Aug 2013 23:59:59 GMT"
my_date.setUTCMonth(my_date.getUTCMonth() + 3);
console.log(my_date.toUTCString());
This outputs "Sun, 01 Dec 2013 23:59:59 GMT"
And now, when I try to only add 2:
my_date.setUTCMonth(my_date.getUTCMonth() + 2);
console.log(my_date.toUTCString());
This outputs "Thu, 31 Oct 2013 23:59:59 GMT"
When I try to set the date to zero:
my_date.setUTCMonth(my_date.getUTCMonth() + 3, 0);
console.log(my_date.toUTCString());
This outputs "Thu, 31 Oct 2013 23:59:59 GMT"
Does anyone know a clean trick for fixing this?
Am I better off ditching the UTC functions and simply removing the timezone offset from all the times? If I did this would it actually fix my problem?

There's no 31 November, so when you add 3 to the months it has no choice but to roll over to the next month, making it 1 December.
The problem of reliably moving forward by months is tricky. You can set the day-of-month (.setDate()) to 1 before doing it, but then you've got to decide how to set it back to something relevant to the original date.

Pointy's answer is correct, here's a function to allow for uneven months:
/* Given a date object, add months (may be +ve or -ve)
** Allow for uneven length months, e.g.
**
** 30 Jan 2013 + 1 month => 30 Feb => 2 Mar
**
** so make 28 Feb. Also works for subtraction
**/
function addMonths(date, months){
// Copy date, avoid IE bug for early dates
var d = new Date(date.getTime());
months = Number(months);
d.setMonth(d.getMonth() + months);
var check = d.getMonth() - date.getMonth() + months;
// If rolled over to next month, go to last day of previous month
if (check) {
d.setDate(0);
}
return d;
}

Related

moment.js - subtract 1 day from Sunday not working as expected

I have a moment object that I want to subtract 1 day from. The original date shows as Sun Jul 15 2018 12:00:00 and I want to subtract 1 day from it so that it outputs as Sat Jul 14 2018 12:00:00.
This seems like it should be really easy if I use the subtract() function, but it's changing the date to the upcoming Saturday, not the Saturday before July 15. I'm assuming this has something to do with the week starting on July 15.
This seems to only be an issue when I'm using Sunday as my starting date. How can I make this work the way I need it to?
Here is my JS:
var timeFormat = 'dddd h:mma';
var originalDate = moment("sunday 12:00:00pm", timeFormat);
var previousDay = moment(originalDate).subtract(1, 'days').format(timeFormat);
var newDate = moment(previousDay+"12:00:00pm", timeFormat);
$(".openTime span").text(originalDate);
$(".newOpenTime span").text(newDate);
This outputs Sun Jul 15 2018 12:00:00 as the originalDate and Sat Jul 21 2018 12:00:00 as the date subtracted by 1 day. As you can see the new date is now Sat Jul 21 for some reason.
Here's a JSFiddle link: https://jsfiddle.net/dmcgrew/b5ev8knd/22/
The problem is that you use a formatted string to create the newDate. The string says something like this:
'Saturday 12:00:00pm'. MomentJs has no information what Saturday you actually mean, so it just takes the next one, which is the 21st of July.
If you just use the previousDay moment and format it, it works:
https://jsfiddle.net/b5ev8knd/36/

JavaScript Date Bug February 2014

So I have this JS-code:
var d1 = new Date();
d1.setFullYear(2014);
d1.setMonth(1);
d1.setDate(1);
Should be Feb-01-2014, right? Only it's not... It returns Mar-01-2014 (actually, the full value is "Sat Mar 01 2014 20:54:29 GMT+0100 (Romance Standard Time)"). What the hell? Same thing happens with any other date value.
If I use this code, however, it works fine:
var d1 = new Date(2014, 1, 1, 0, 0, 0, 0);
The result is: Sat Feb 01 2014 00:00:00 GMT+0100 (Romance Standard Time)
Any ideas what's going on?
Here's what's happening, line for line:
You create a new date object with today's date.
var d1 = new Date(); // d1 = 2014-04-30
Then you set the year to 2014, which it already is, so nothing really happens.
d1.setFullYear(2014); // d1 = 2014-04-30
Here's the tricky part, because now you change the month to February. But this would make the date February the 30th (2014-02-30) which doesn't exist, so the JavaScript will try to find the closest valid date which is first of March (2014-03-01).
d1.setMonth(1); // d1 = 2014-02-30 is not valid so JS makes it 2014-03-01
Then you set the day to the first day of the month, which it already is, so nothing really happens here either.
d1.setDate(1) // d1 = 2014-03-01
You need to call setDate first. Basically it's grabbing the month and using the current date and since February doesn't have a 30th, it's defaulting to March.
Better to initialize Date, rather than have it default to the current date.
var d1 = new Date(0); // 1 January 1970 00:00:00 UTC
Try this:
d1.setFullYear(2014);
d1.setDate(1);
d1.setMonth(1);
What you were doing:
d1.setFullYear(2014); // change year to 2014 (30 Apr 2014 -> 30 Apr 2014)
d1.setMonth(1); // change month to 1 (30 Apr 2014 -> 30 Feb 2014, really 2 Mar 2014)
d1.setDate(1); // change day of month to 1 (2 Mar 2014 -> 1 Mar 2014)
By setting the date first, you're changing the date to 1 Apr 2014 before changing the month.

javascript is creating date wrong month

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Add hours to javascript date object bug?

Hi guys so I've a wierd bug that I can't figure out. I create a date object from a date chooser and a set of combos with hours/mins.
Now the problem is when it's March 30th 2013 and 1pm adding 12 hours only adds 11 for some reason. With any other day like March 31st 2013 at 1pm this is fine. See example below
var d = new Date(1364601600000)
d.setHours(13)
d.setMinutes(13)
console.log(d)
d.setHours(d.getHours() + 12)
console.log(d)
console.log('--')
var d2 = new Date(1364688000000)
d2.setHours(13)
d2.setMinutes(13)
console.log(d2)
d2.setHours(d2.getHours() + 12)
console.log(d2)
See an example: http://jsfiddle.net/k8L2W/2/
The daylight scheme for the year 2013 works as follows :
Sunday, 31 March 2013, 01:00:00 clocks are turned forward 1 hour to
Sunday, 31 March 2013, 02:00:00 local daylight time instead
For reference, please check here

Javascript 30 Nov 2010 plus 3 months equal to 2 Mar 2011

with this script
var d = new Date(2010, 10, 30);
var e = new Date(d.getFullYear(), d.getMonth() + 3, d.getDate());
document.write(d + "<br>" + e);
why 30 Nov 2010 plus 3 months equal to 2 Mar 2011? not 28 Feb 2011?
30 Nov 2010 and 23 Feb 2011 are the last day of the month.
http://jsfiddle.net/jWh2M/
In your example, the date you specify is
30 Feb 2011
that gets converted into
2 Mar 2011
which kind of makes sense, doesn't it?
You should definitely choose a different method of adding the time span.
Either calculate the last day of each month explicitly, or alternatively, use a date library like date.js. I haven't used that one myself, but SO user #CMS recommends it, that's good enough for me.
Looking at the syntax, this might work in date.js:
Date.parse('November 30th 2010 + 3 months');
You can get the last day of a month by using this function here:
function daysInMonth(iMonth, iYear)
{
return 32 - new Date(iYear, iMonth, 32).getDate();
}
And set the new day to it, if your month 'overflows'.

Categories

Resources