Finding the average time between an array of timestamps in javascript - javascript

I'm trying to get the average time between these timestamps. But it keeps coming out as
"average = ", "Wed Dec 31 1969 20:48:03 GMT-0800 (Pacific Standard Time)"
How can I get the correct average between the dates below ?
Adding up the sum of all the dates is not going to give me the average between the first date and the last date.
Here is my code below.
const timeStamps = [
"Tue Aug 02 2022 09:47:39 GMT-0700 (PST)",
"Mon Aug 01 2022 09:47:39 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:42 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:02 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:55 GMT-0700 (PST)",
];
const sortedDates = timeStamps.map((t) => new Date(t)).sort((a, b) => a - b);
const average =
(sortedDates[sortedDates.length - 1] - sortedDates[0]) / sortedDates.length -
1;
console.log("average = ", new Date(average).toString());

Try this:
const timeStamps = ['Tue Aug 02 2022 09:47:39 GMT-0700 (PST)', 'Mon Aug 01 2022 09:47:39 GMT-0700 (PST)', 'Tue Aug 02 2022 09:47:42 GMT-0700 (PST)', 'Tue Aug 02 2022 09:47:02 GMT-0700 (PST)', 'Tue Aug 02 2022 09:47:55 GMT-0700 (PST)' ]
const ts = timeStamps.map(time => new Date(time).getTime());
const sortedDates = ts.sort((a, b) => a - b);
const average =
(sortedDates[sortedDates.length - 1] + sortedDates[0]) / 2;
console.log("average = ", new Date(average));

The arithmetic mean or the average is the sum of all the numbers divided by the count of numbers in any collection.
We could sum up all the timestamps and divide it with the array length.
Note that the items in the timestamps array are strings, we could get their UNIX timestamp with new Date(timestamp).valueOf().
const timestamps = [
"Tue Aug 02 2022 09:47:39 GMT-0700 (PST)",
"Mon Aug 01 2022 09:47:39 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:42 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:02 GMT-0700 (PST)",
"Tue Aug 02 2022 09:47:55 GMT-0700 (PST)",
];
let sum = 0;
for (let i = 0; i < timestamps.length; i++) {
sum += new Date(timestamps[i]).valueOf();
}
const average = sum / timestamps.length;
console.log(average); // or console.log(new Date(average));

Related

Google Apps Script return date between a range of dates in an array

I have an array of dates. I want to return the dates between January 01, 2022 and December 31, 2022.
When I run the cut below, it doesn't return anything. This is one of many different loop variations I've tried to no avail. Please help TIA
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
...](**output** NOT the actual script)
for(var i=0; i<=arr.length; i++){
if(arr[i] >= start && arr[i] <= end){
Logger.log(arr[i]);
}
}
Modification points:
From your script, I couldn't understand the value of arr. Because in your script, when you save the script, an error occurs. So I'm worried that your actual script is different from your showing script. If each value of arr is the string type like Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time), please enclose it using the double or single quotes.
If my guess of your script is not correct, please provide your actual script.
In order to compare the date, I think that this thread will be useful. Ref
When these points are reflected in your script, it becomes as follows.
Modified script:
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [
"Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time)",
"Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time)",
];
for (var i = 0; i <= arr.length; i++) {
if (new Date(arr[i]).getTime() >= start.getTime() && new Date(arr[i]).getTime() <= end.getTime()) {
console.log(arr[i]);
}
}
References:
Related thread
Compare two dates with JavaScript
What Tanaike mentioned is correct, it should be a string in your array and missing quotes then you can do
arr.filter(dates => new Date(dates) >= start && new Date(dates) <= end );
I was able to figure it out thanks for your input. The problem was I needed to set arr[i] to new Date(arr[i]). Below is the updated code.
var start = new Date(2022, 0, 01); //returns Sat Jan 01 00:00:00 GMT-06:00 2022
var end = new Date(2022, 11, 01); //returns Sat Dec 31 00:00:00 GMT-06:00 2022
var arr = [Fri Dec 03 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 10 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 17 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 24 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Dec 31 2021 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 07 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 14 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 21 2022 00:00:00 GMT-0600 (Central Standard Time),
Fri Jan 28 2022 00:00:00 GMT-0600 (Central Standard Time),
...](**output of the array** NOT the actual script)
for(var i=0; i<=arr.length; i++){
var arrI = new Date(arr[i]);
if(arrI >= start && arrI <= end){
Logger.log(arrI);
}
}

Javascript: Removing part of a string ( get date string )

I simply need to remove the time from this string Sun Apr 26 2020 01:00:00 GMT+0100 (BST)
Current solution which works
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
const dateTimeArray = dateTime.split(' ');
const date = dateTimeArray.splice(0, 4);
console.log(date.join(' ')); // Correctly returns 'Sun Apr 26 2020'
Although this works I'm wondering if theres a more elegant? Or perhaps a regex?
You can use toDateString
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
console.log(new Date(dateTime).toDateString())
You could try
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
const date = dateTime.substring(0, 15);
console.log(date);
Just use String.substring():
const dateTime = 'Sun Apr 26 2020 01:00:00 GMT+0100 (BST)';
console.log(dateTime.substring(0, 15));

How to Format JavaScript Long String Date

I am using this code to export dates of current week days
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toString());
}
but the output looks like Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time) in the array of
["Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov
09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015
01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57
GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800
(Pacific Standard Time)"]
Can you please let me know how I can format the date() to get only
Mon Nov 09 2015 and remove 01:43:57 GMT-0800 (Pacific Standard Time)?
Thanks
You can use the toDateString() method on the Date object.
arr.push(next.toDateString());
Its simple. I am using your code. just use toDateString() method of Date()
var arr =[];
var curr = new Date();
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
var next = new Date(curr.getTime());
next.setDate(first+1 );
arr.push(next.toDateString());
}
document.write(new Date().toDateString());

Split an array of date times into sub dates in javascript

I have a simple data set that contains an array of times that are separated into 3 hour chunks:
0: Wed Dec 04 2013 12:00:00 GMT-0800 (PST)
1: Wed Dec 04 2013 15:00:00 GMT-0800 (PST)
2: Wed Dec 04 2013 18:00:00 GMT-0800 (PST)
3: Wed Dec 04 2013 21:00:00 GMT-0800 (PST)
4: Thu Dec 05 2013 00:00:00 GMT-0800 (PST)
5: Thu Dec 05 2013 03:00:00 GMT-0800 (PST)
6: Thu Dec 05 2013 06:00:00 GMT-0800 (PST)
7: Thu Dec 05 2013 09:00:00 GMT-0800 (PST)
What I need to do is loop through this array and create a new array that adds in the "missing" hours. So ideally the new array would be an array of 21 new date objects, separated out by each hour.
Edit:
Tried looping through each item in the array, and then looping through 3 times within there and try to create a new date and push it into a new array. But I'm stuck there.
After some snooping and researching some date stuff, was able to figure out what I needed to do. (open to suggestions if there are better ways to do this)
var oldArrayLength = oldArray.length;
var newArray = [];
for (var i = 0; i < oldArrayLength; i++) {
for( var j = 0; j < 3; j++ ) {
var theNewTime = new Date(oldArray[i]);
theNewTime.setHours(theNewTime.getHours() + j);
newArray.push(theNewTime);
}
}

date loop increment and growing variable ( mootools )

Can someone please take a look at this with fresh eyes.
var start_date = Date.parse('2013-07-01');
var i_date = Date.parse('2013-07-5');
console.log(start_date + '---before loop ');
for (var n = start_date; n < i_date; n.increment()) {
console.log(start_date + '---inside loop ');
}
console.log(start_date + '---after loop ');
This code produces this:
Mon Jul 01 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---before loop
Mon Jul 01 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Tue Jul 02 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Wed Jul 03 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Thu Jul 04 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---inside loop
Fri Jul 05 2013 00:00:00 GMT+0200 (W. Europe Daylight Time)---after loop
Why does start_date variable grow?
(fiddle here if needed)
The problem is that n and start_date are pointing to the same object. You need to clone the date by creating new Date object, for example:
n = new Date(start_date);
Updated demo.
Example:
> a = new Date()
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> b = a
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> c = new Date(a)
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
// Do some stuff with "a"
> a
Sat Jun 29 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> b
Sat Jun 29 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)
> c
Sun Jul 07 2013 19:51:09 GMT+0600 (Ekaterinburg Standard Time)

Categories

Resources