Sorry if this is a newbie question... I have this working correctly:
let date = new Date(2021, 0, 12);
dates = [];
for (let i = 0; i < 30; i++) {
date.setDate(date.getDate() + 1);
const dateString = date.toLocaleDateString();
dates.push(dateString);
}
console.log(dates);
All good. But without the new variable 'dateString', I see a funny behavior I don't quite understand. Initially I tried to do:
let date = new Date(2021, 0, 12);
let dates = [];
for (let i = 0; i < 30; i++) {
date.setDate(date.getDate() + 1);
date = date.toLocaleDateString(); // change same var instead of new var
dates.push(date);
}
console.log(dates);
This throws the following error: TypeError: date.getDate is not a function. Why should the behavior of the date object change in reverse?
Related
I am trying to get the next upcoming Time(In hours) from an array of times. The way it should work is it goes through the array of times (5:00, 10:00, etc.) and determines which will come next based on the current time. These times are in UTC.
Right now, what I've tried doing is making an array of dates on that day with those times. Depending on the time, however, this could produce an array of dates in which all of have already passed. To prevent this after checking to find the closest time, if the index is negative, it adds a day to every date in the array and then loops through it again.
Currently, my code just gives me the current time. Could anyone help me figure out how to make it work as described in the first paragraph?
My Current Code:
let dates = [new Date().setHours(5, 0, 0, 0), new Date().setHours(10, 0, 0, 0), new Date().setHours(15, 0, 0, 0), new Date().setHours(20, 0, 0, 0)]
let index = null;
for (var i = 0; i < dates.length; i++) {
if (!index || dates[i] - new Date() < index) {
index = dates[i] - new Date()
}
}
if (Math.sign(index) == -1) {
index = null;
for (var i = 0; i < dates.length; i++) {
dates[i] = dates[i] + 86400000;
}
for (var i = 0; i < dates.length; i++) {
if (!index || dates[i] - new Date() < index) {
index = dates[i] - new Date()
}
}
console.log(new Date(new Date() + index))
}
After some more research I was able to solve my question. There were many changes that needed to be made. The following code works as intented.
let dateObject = new Date();
let dates = [dateObject.setUTCHours(5, 0, 0, 0), dateObject.setUTCHours(10, 0, 0, 0), dateObject.setUTCHours(15, 0, 0, 0), dateObject.setUTCHours(20, 0, 0, 0)]
let nextDate = null;
// Get the current date and time in UTC
let now = new Date();
now.setUTCDate(now.getUTCDate());
now.setUTCHours(now.getUTCHours());
now.setUTCMinutes(now.getUTCMinutes());
now.setUTCSeconds(now.getUTCSeconds());
// Loop through the dates array
for (let date of dates) {
// Convert the date from a number to a Date object
date = new Date(date);
// If the date is later than the current date, log it and break out of the loop
if (date > now) {
nextDate = date;
break;
}
}
if (!nextDate) {
nextDate = new Date(dates[0] + 86400000);
}
console.log(nextDate);
Here I'm looping the date to get past 50 days date!!
but the problem is I only want the current date time as the current time and next from there all date time I want as 12:00:00 !!
const dates = [];
const today = dayjs(new Date());
for (let d = 0; d < 50; d++) {
dates.push(today.subtract(d, "day"));
}
console.log(dates);
const dates = [];
for (let d = 0; d < 50; d++) {
const date = new Date()
if (d !== 0) {
date.setHours(12, 0, 0)
}
dates.push(date)
}
console.log(dates);
const dates = [];
const date = new Date();
for (let i = 0; i < 50; i++) {
var currentDate = new Date();
if (i !== 0) {
currentDate.setDate(currentDate.getDate() + i);
currentDate.setHours(12, 0, 0);
}
dates.push(currentDate);
}
console.log(dates);
Hi i have this script ( vue.js ) i can see the next 2 weeks works good i must change that i can see 2 weeks on the past ..
Thanks
methods: {
// Get all days without sunday:
dates(index) {
var week = new Array();
let current = new Date();
// Starting Monday not Sunday
current.setDate((current.getDate() - current.getDay() +1));
for (var i = 0; i < 13; i++) {
let date = new Date(current);
week.push(moment(date).format('DD.MM.YY'));
current.setDate(current.getDate() +1);
}
return week[index];
},
If you want to go back in time, you need to subtract from the current date:
methods: {
// Get all days without sunday:
dates(index) {
var week = new Array();
let current = new Date();
// Starting Monday not Sunday
current.setDate((current.getDate() - current.getDay() +1));
for (var i = 0; i < 13; i++) {
let date = new Date(current);
week.push(moment(date).format('DD.MM.YY'));
current.setDate(current.getDate() - 1); // <-- this line changed
}
return week[index];
},
Try this:
function dates(index) {
var week = new Array();
let current = moment().subtract(1, 'days');
for (var i = 0; i < 12; i++) {
week.push(current.format('DD.MM.YY'));
current = current.subtract(1, 'days')
}
console.log(week);
return week[index];
}
console.log(dates(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
i try to make array of dates ,but when i push the last one all the other dates in the array be the same last one .
var dateArayy=[];
var date = new Date();
function addone(){
date.setDate(date.getDate()+1); //add day to the date
dateArayy.push(date) ;
// i try also dateArayy[dateArayy.lenght]=date THE SAME..
}
for (let i=1;i<10;i++){
addone();
}
console.log(dateArayy)
you change and set the same object in array . you should create new Date object and add it to the array
var dateArayy = [];
var date = new Date();
function addone() {
var newdate = new Date();
newdate.setDate(date.getDate() + 1);
date = newdate;
dateArayy.push(date);
}
for (let i = 1; i < 10; i++) {
addone();
}
console.log(dateArayy);
let dates = [];
for (let i = 0; i < 10; i++) {
let date = new Date();
date.setDate(date.getDate() + i);
dates.push(date);
}
console.log(dates);
I have a React component where I have a date string from which I need to generate an array of dates representing the next 11 days excluding the starting date (10/12/2016). Not sure how to achieve this. That's what I've tried so far but the problem is that by simply looping adding 1 for each iteration on the day, it won't generate the correct date when the date range of 11 days spans between two months:
addDays = () => {
const { startDate } = this.props.pageData.parcelDetails.parcelDetails;
const date = new Date(startDate);
let datesCollection = []
for (var i = 1; i < 12; i++) {
datesCollection.push(`${date.getDate() + i}/${date.getMonth() + 1}/${date.getFullYear()}`)
}
return datesCollection
}
The code above generates the following array:
[
"11/12/2016",
"12/12/2016",
"13/12/2016",
"14/12/2016",
"15/12/2016",
"16/12/2016",
"17/12/2016",
"18/12/2016",
"19/11/2016",
"20/12/2016",
"21/12/2016"
]
How do I generate the correct array, with proper dates for each month?
You can simply do that:
addDays = () => {
const { startDate } = this.props.pageData.parcelDetails.parcelDetails;
const date = new Date(startDate);
let datesCollection = []
for (var i = 1; i < 12; i++) {
const newDate = new Date(date.getTime() + i * 1000 * 60 * 60 * 24);
datesCollection.push(`${newDate.getDate()}/${newDate.getMonth() + 1}/${newDate.getFullYear()}`);
}
return datesCollection
}
You can try adding 1 day to each loop.
var dateuse = new Date();
dateuse.setDate(dateuse.getDate() + 1);
or you can try Moment.js
var today = moment();
var dateuse = moment(today).add(1, 'days');