I have just noticed some strange behavior in one of my Arrays. I am sure the issue is with how Javascript stores object references in arrays. I will demonstrate the issue with a bit of code I posted as an answer to another question on SO. The code, just loops to get todays date and the 6 previous dates of the month, pretty self explanatory.
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
dates.push(tempDate);
}
console.log(dates);
Output: [Thu Jun 05 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Wed Jun 04 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Tue Jun 03 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Mon Jun 02 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Sun Jun 01 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Sat May 31 2014 14:54:14 GMT+0100 (GMT Daylight Time),
Fri May 30 2014 14:54:14 GMT+0100 (GMT Daylight Time)]
This is correct, and is expected, as tempDate is continually recreated as a new Date object inside of the loop.
When I take tempDate out of the loop however, it seems to update all of the objects in the array every iteration of the loop (also the loop seems to go one Month and 1 day too far to 29th apr):
var dates = [];
var date = new Date();
var tempDate = new Date();
for (var i = 0; i < 7; i++){
tempDate.setDate(date.getDate()-i);
dates.push(tempDate);
}
console.log(dates);
Output: [Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time),
Tue Apr 29 2014 14:52:21 GMT+0100 (GMT Daylight Time)]
So the two questions I pose are:
Why does the object stored in the array keep mutating with every
iteration? (I suspect this is because of the way Javascript is
storing the references to the object, but an explanation would be
nice)
Why does the loop run one too far in the second bit of code? The first example shows the loop ends after 7 successful iterations, on May the 30th (7 days in the past), the second example shows the resultant date after the iterations as the 29th april - more than a month into the past. Why?
edit: I have a rudimentary jsfiddle, to allow testing the code.
var tempDate = new Date(); creates new object. so in this example:
var array = [tempDate,tempDate,tempDate,tempDate,tempDate];
array[0] is same object as array[1]
this is just how objects works in almost every programming language:
var a = tempDate;
var b = tempDate;
var c = b;
(in above example a, b, c, and tempDate is same thing)
Regarding question 2:
please have a look at this fiddle http://jsfiddle.net/Mqnmm/
with new object tempDate is always today, then you apply setDate()
with old object, before last literation date is Sat, 31 May 2014 14:23:34 GMT, then you apply tempDate.setDate(-1) which sets date to last N days of previous month
Subtract days from a date in JavaScript please check Rob Dawley`s answer to properly adjust dates
Related
I ran into a problem with a 'time' in JS. So basicaly I'm tring to get a time from databace as a string like 11:00 and 20:30.
With the upcoming function code I convert it to js format:
function getDateFromHours(time) {
time = time.split(':');
let now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), ...time);
}
After all I got this: Mon Nov 09 2020 11:00:00 GMT+0300 and this: Mon Nov 09 2020 20:30:00 GMT+0300
So the first question:
How to compare these times to each other?
How to output every 15 minuties between these two times in concole to make it look like:
Mon Nov 09 2020 11:00:00 GMT+0300
Mon Nov 09 2020 11:15:00 GMT+0300
Mon Nov 09 2020 11:30:00 GMT+0300
etc...
For comparing times, and generating times algorithmically, you're best to work in timestamps. It's just a matter of converting between milliseconds and whatever unit of time you care to think in. You can get the timestamp from a Date object with the getTime method.
I've been struggling for days with some DateTime values.
I have an API backend that uses entity framework and sql server with .netcore.
The big issue when i want to send a datetime from angular to c#
backend. I noticed that Date() in typescript/javascript by default
uses my timezone and i don't know how to exclude it.
For example my date looks like this:
Wed Jul 11 2019 21:00:00 GMT+0300
And when it arrived in c# it becomes 07/10/2010(mm-dd-yyyy), it subtracts 1 day due to timezone.
Is there a way to standardize the Date variable to ignore timezone and always keep the same format DD-MM-YYYY ?
I've also tried to use MomentJS and still can't figure it out, even my MomentJS compares are acting strange due tot his issue.
For example:
const VacationStart = moment(calendarEntity.Vacation.StartTime).utc(false);
const VacationEnd = moment(calendarEntity.Vacation.EndTime).utc(false);
if (VacationStart.isSameOrBefore(ColumnDate,'day') && VacationEnd.isSameOrAfter(ColumnDate,'day')) {
return '#FF0000';
}
In the above example:
VacationStart is Wed Jul 10 2019 21:00:00 GMT+0300
VacationEnd is Wed Jul 17 2019 00:00:00 GMT+0300
ColumnDate is Thu Aug 15 2019 03:00:00 GMT+0300 (incremental value)
Yet for some reason even if i use isSameOrBefore(ColumnDate,'day') to specify to compare only up to days it still does not work. When VacationEnd should be equal to ColumnDate is return false.
Note: everything is in a foreach loop where ColumnDate increases by +1 day.
You just need to use UTC time (Greenwich Mean Time)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.utcnow?view=netcore-2.2
So something like this:
new Date(new Date().toUTCString()); -- "Mon Jul 01 2019 17:55:41 GMT-0700 (Pacific Daylight Time)"
new Date().toUTCString(); -- "Tue, 02 Jul 2019 00:56:38 GMT"
new Date().toString(); -- "Mon Jul 01 2019 17:57:03 GMT-0700 (Pacific Daylight Time)"
I get inconsistent timezone based on params to Date():
new Date()
Sun Oct 25 2015 18:10:42 GMT+0200 (IST)
new Date(1445720400)
Sat Jan 17 1970 19:35:20 GMT+0200 (IST)
new Date(144572040000)
Thu Aug 01 1974 09:54:00 GMT+0300 (IDT)
new Date(14457204000000)
Thu Feb 17 2428 20:00:00 GMT+0200 (IST)
I tried reading the docs or finding an explanation to this weirdness, but couldn't.
I've checked on both Chrome 46 and Safari 7.1.8,
Any ideas?
Isn't this just daylight savings? One of the dates happened to be in the summer?
The problem in then you set different time in ms as param for 'new Date()'. And you have different time zones because the Date has been generated in different seasons (Summer's time and Winter's time). It is normal.
I got one weird issue with Date object initialization. And wondering if someone can explain why..
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
console.log(exp1);
console.log(exp2);
console.log(exp3);
Results:
Thu Oct 16 2014 18:00:00 GMT-0600 (MDT) // 16th?
Fri Oct 17 2014 00:00:00 GMT-0700 (MST) // Why GMT -7
Fri Oct 17 2014 00:00:00 GMT-0600 (MDT) // The only one that works as expected
Why are these three Date objects so different?
The first date is treated as GMT since no time zone offset is provided. When logged out it shows the time in your local timezone. Adding an offset (exp4 below), I get the date expected.
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
var exp4 = new Date('2014-10-17z-0500');
Results:
Thu Oct 16 2014 19:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
I am not sure about exp2 for you, but suspect it has something to do with daylight savings time and that you live in an area that does not observe daylight savings (Arizona?).
Edit: this seems to be browser specific. The results above were generated in Chrome while in IE 11, exp4 was an invalid date. For IE 11 I had to use this format:
var exp4 = new Date('2014-10-17T00:00-05:00');
In the library D3. I find the set of functions to handle dates a bit inconsistent. For example, doing the following 4 steps in a console of a page loading D3 I get:
> start = new Date(2010, 11, 30)
Thu Dec 30 2010 00:00:00 GMT+0000 (GMT Standard Time)
> end = new Date(2011, 0, 2)
Sun Jan 02 2011 00:00:00 GMT+0000 (GMT Standard Time)
> d3.time.months(start, end, 1)
[Sat Jan 01 2011 00:00:00 GMT+0000 (GMT Standard Time)]
> d3.time.days(start, end, 1)
[Thu Dec 30 2010 00:00:00 GMT+0000 (GMT Standard Time), Fri Dec 31 2010 00:00:00 GMT+0000 (GMT Standard Time), Sat Jan 01 2011 00:00:00 GMT+0000 (GMT Standard Time)]
the above indicates that day.range starts from the first item and ends just before the second, while month.range seems to do the opposite.
In the documentation it's stated:
# d3.time.months(start, stop[, step])
Alias for d3.time.month.range. Returns the month boundaries (e.g., January 01)
after or equal to start and before stop. If step is specified, then every step'th
month will be returned, based on the month of the year. For example, a step of 3
will return January, April, July, etc.
after or equal to start and before stop is also mentioned for time.days but the result appears to be different. Also, when these functions return after and when equal to the start? What makes the difference?
NB: my wish would be having these functions returning arrays of days, months, years including both start and end parameters.
As clearly explained in here the behaviour is in fact consistent. Both day.range and month.range aim to return each daily and monthly boundaries between the start and end parameter.