How to Format JavaScript Long String Date - javascript

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

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 - 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 sort date array with javascript?

I am trying to sort the date array. It does perfectly. But May 4 is listed in the last element of the array which is wrong. Could you help me to resolve this?
var listOfString = "2016-05-24|Failed,2016-05-04|Failed,2016-04-19|Passed,2016-04-15|Passed,2016-06-01|Failed";
var arrayStrings = listOfString.split(',');
var dateArray = new Array();
var count = 0;
for (var i = 0; i < arrayStrings.length; i++) {
dateArray[count++] = new Date(arrayStrings[i].split('|')[0]);
}
dateArray.sort();
for (var i = 0; i < dateArray.length; i++) {
alert(dateArray[i]);
}
Output received as
Fri Apr 15 2016 05:30:00 GMT+0530 (India Standard Time)
Tue Apr 19 2016 05:30:00 GMT+0530 (India Standard Time)
Tue May 24 2016 05:30:00 GMT+0530 (India Standard Time)
Wed Jun 01 2016 05:30:00 GMT+0530 (India Standard Time)
Wed May 04 2016 05:30:00 GMT+0530 (India Standard Time)
Since you have the values as strings in descending order (y-m-d) and with preceding zeroes it means they are sortable also as strings. You don't need to do any conversions in this case, just sort as a string array after split.
Solution:
var listOfString = "2016-05-24|Failed,2016-05-04|Failed,2016-04-19|Passed,2016-04-15|Passed,2016-06-01|Failed";
var arrayStrings = listOfString.split(',');
var dateArray = new Array();
var count = 0;
for (var i = 0; i < arrayStrings.length; i++) {
dateArray[count++] = arrayStrings[i].split('|')[0];
}
dateArray.sort();
for (var i = 0; i < dateArray.length; i++) {
console.log(dateArray[i]);
}
Change your date.sort() to this
dateArray.sort(function(a,b) {
return a-b;
});
Returns
VM126:57 Fri Apr 15 2016 05:30:00 GMT+0530 (IST)
VM126:57 Tue Apr 19 2016 05:30:00 GMT+0530 (IST)
VM126:57 Wed May 04 2016 05:30:00 GMT+0530 (IST)
VM126:57 Tue May 24 2016 05:30:00 GMT+0530 (IST)
VM126:57 Wed Jun 01 2016 05:30:00 GMT+0530 (IST)
You assumed that .sort() would automatically handle dates but unfortunately, that's not the case. It either does alphabetical or numerical sort only and that was the result you had. To fix, I added a callback function that will determine if an item is ahead or behind in the order in the bubble sort and subtracting dates returns the number of seconds between the two dates and that would be a number and easy to subtract and return the difference in the sort.

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

Categories

Resources