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

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));

Related

Finding the average time between an array of timestamps in 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));

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 - date difference should be zero but it is 18 hours

This one has stumped me. It should be so simple I would think. I am doing some very simple date subtraction in Javascript. I am subtracting the same dates and I would think it would give zero hours, but it gives 18 hours.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime();
document.getElementById('date').innerHTML = new Date(outDate - inDate);
<div id='date'>
</div>
In case it produces different results based on where you are, the result I am getting is this:
Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
This is due to your timezone. If you convert to GMT String before print it the time will be correct. (Jan 01, 1969 00:00:00)
new Date(outDate - inDate).toGMTString()
You should see the correct date.
let inDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
let outDate = new Date('Tue Aug 27 2019 00:00:00 GMT-0500 (Central Daylight Time)').getTime()
console.log(new Date(inDate - outDate).toGMTString())

How to use .replace to replace the string in a array

need to replace GMT+0530 (India Standard Time) to IST
dynamically for multiple array list
for now my array list has 6 entries. need to replace for all the array list .
function getTimeAccordingtoTimeZone(utc){
utc = new Date(Date.parse(utc));
var dateUTC = utc ;
var dateIST = new Date(dateUTC);
//date shifting for IST timezone (+5 hours and 30 minutes)
var current_time_zone = getCurrentTimeZone();
var hour_diff = parseInt(current_time_zone);
var minute_diff = current_time_zone - hour_diff;
minute_diff = minute_diff*60;
dateIST.setHours(dateIST.getHours() + hour_diff);
dateIST.setMinutes(dateIST.getMinutes() + minute_diff);
var new_date = dateIST;
return new_date;
}
new_date returns
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
Tue Jan 15 2019 22:49:04 GMT+0530 (India Standard Time)
I'd propose you use moment JS to format the string.
In your case, the following code will help you:
const moment = require('moment');
date = moment();
const dateString = `${date.format('ddd MMM DD YYYY HH:mm:ss')} IST`
console.log(dateString);
MomentJs Documentation

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());

Categories

Resources