Can't advance day at 2012/10/20 - javascript

Try this:
date = new Date(2012,9,20)
Sat Oct 20 2012 00:00:00 GMT-0300 (BRT)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)
Sat Oct 20 2012 23:00:00 GMT-0300 (BRT)
(tested on Chrome and Firebug)
But this works:
date = new Date(2012,10,20)
Sat Nov 20 2012 00:00:00 GMT-0300 (BRT)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)
Sat Nov 21 2012 0:00:00 GMT-0300 (BRT)

The problem is that daylight saving's time started on Oct 20 in Brazil (BRT). Try using UTC time:
date= new Date(Date.UTC(2012,9,20)); // zero-based month: 9->october
new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()+1))
This should make your date advancement independent of daylight saving's time, allowing for your transition to happen smoothly. For more information, see Javascript dates: what is the best way to deal with Daylight Savings Time?
Alternatively, you could set your date's time to something in the middle of the day, like, say, noon, if all you really care about is the day.
date = new Date(2012,9,20,12)
new Date(date.getFullYear(), date.getMonth(), date.getDate()+1)

It is daylight saving time: at this time in GMT-0300 time is rewinded 1 hour backwards.
Your sample is working fine in GMT+0400 timezone.
See question 1º Day of Daylight Saving Time Java and JS showing a different behavior

It works just fine:
console.log(date = new Date(2012,9,20))
console.log(new Date(date.getFullYear(), date.getMonth(), date.getDate()+1))
// returns:
// Sat Oct 20 2012 00:00:00 GMT+0200 (West-Europa (zomertijd))
// Sun Oct 21 2012 00:00:00 GMT+0200 (West-Europa (zomertijd))
(Don't mind the localized string at the end)
Apparently you just hit the day daylight saving's time started in your localization. try Date.UTC(), instead, unless you specifically need the time for your localization.

Related

Why does JavaScript Date(0) return 1am GMT+1 Jan 1st 1970?

I am in London (GMT time zone). We are currently in DST.
In the UK, Daylight Saving Time starts in March and ends in October, so Jan 1 1970 would have been outside DST.
However,
epochDate = new Date(0);
console.log(epochDate);
returns
Thu Jan 01 1970 01:00:00 GMT+0100 (Greenwich Mean Time)
I expected
Thu Jan 01 1970 00:00:00 GMT+0000 (Greenwich Mean Time)
Does Date(); return results in the current timezone?!
JavaScript Date(0) returns GMT+1 because, according to Wikipedia (and timeanddate.com), "the British Standard Time experiment, with Britain remaining on GMT+1 throughout the year… took place between 27 October 1968 and 31 October 1971"

How to round of date with momentjs?

1) My Accurate date is :Tue Apr 25 2019 00:00:00 GMT+0530
2) But in Calendar when i am selecting time i was get like this
Tue Apr 24 2019 16:56:00 GMT+0530
I was tried to fix below way
moment($scope.ProjectModel.projectStartDate).startOf('day')
But I am getting Tue Apr 24 2019 00:00:00 GMT+0530
This one is not correct, correct date: Tue Apr 25 2019 00:00:00 GMT+0530
So what i need to do to get this date ?
So, it seems like you want to set the date to the start of the next day, rather than the start of the current one. You can do it like this:
moment($scope.ProjectModel.projectStartDate).startOf('day').add(1, 'days');
Or, it seems like you want to round up if the provided date is noon or later, and down otherwise. In this case, just add 12 hours before going to the start of the day:
moment($scope.ProjectModel.projectStartDate).add(12, 'hours').startOf('day');

Google sheets custom function returning wrong time difference

I'm writing custom function for google sheets.
One part should calculate delta of two difference times.
function timeOut(input) {
var difference = input[0][1]-input[0][0];
var output = new Date(difference);
return output;
}
Variable difference contains zero.
The problem is that new Date(0) is returning
Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
which instead of
Thu Jan 01 1970 00:00:00 GMT+0100 (CET)
So difference of 9 hours minus 9 hours is 1 hours, which is obviously wrong.
Could you pinpoint me, what I'm doing wrong ?
00:00:00 GMT+0100 would be -1 hour since it is +0100, the timezone matters, 01:00:00 GMT+0100 really is 0 hours.
If you want to get GMT, you can use toGMTString():
var date = new Date(0);
alert(date.toGMTString()); // prints Thu, 01 Jan 1970 00:00:00 GMT
It's actually obviously correct, on the 00:00:00 of the GMT time, in +0100 the time was 01:00:00.
If you need the GMT time you can:
var GMTTime = new Date( -new Date().getTimezoneOffset() * 60000 );

javascript date subtract hours - Error

I'm trying to subtract 1 hour from the current date, I'm using this code:
var date = new Date();
// date is **Fri Apr 03 2015 16:47:33 GMT+0100 (GMT Standard Time)**
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1)).toISOString();
//now date is **Fri Apr 03 2015 14:47:33 GMT+0100 (GMT Standard Time)**
There's less two hours instead of just one. What am I missing here?
Your code works when looking at the elements in the same format.
<div class="original"></div>
<div class="updated"></div>
Using jQuery to post the information ... with your code, as is.
$(".original").text(date.toISOString());
$(".updated").text(uploadDateFilter);
Results in ...
2015-04-03T16:00:23.441Z
2015-04-03T15:00:23.441Z
http://jsfiddle.net/rfornal/5nma5uhr/
You don't need .toISOString(), but for me it works :
var date = new Date();
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1));
console.log(date);
console.log(uploadDateFilter);
Console output :
Fri, 03 Apr 2015 15:59:48 GMT
Fri, 03 Apr 2015 14:59:48 GMT
http://repl.it/gxL
The reason appears to be daylight saving check this my fiddle.
For me in the UK the clocks went forward on Mar 29, 2015. Checking the effect of new Date().toISOString() before and after this date, during daylight saving the result is one hour less.

Why is new Date() subtracting 1 day in Javascript?

I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.

Categories