show 2 week in the past ( vue.js) - javascript

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>

Related

How to get next date time start from 12:00:00 in javascript

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

Date Number increases wrong in JavaScript

I have a problem with creating a calendar in JavaScript. I want to display dates from Monday to Sunday. So this is my DateSet function:
dateSets(year) {
const today = new Date();
year = today.getFullYear();
let firstDayOfWeek = this.getDateOfWeek(this.currentWeekNumber, year);
let dates = [];
for (let i = 0; i < 7; i++) {
dates.push(
new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + i))
);
}
return dates;
}
getDateOfWeek(this.currentWeekNumber, year) gets the Monday of the week. But with this code displays 9/8/2021 10/8/2021 12/8/2021 15/8/2021 19/8/2021 24/8/2021 30/8/2021.
So when I change the i in the for with 1 it displays like this but I cannot use this because I cannot get Monday: 10/8/2021 11/8/2021 12/8/2021 13/8/2021 14/8/2021 15/8/2021 16/8/2021.

JS : add date obgectto array

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

Get a list of dates between two dates using javascript

From JavaScript is there a way to get list of days between two dates from MySQL format. I don't want to use any library for this.
This is what i did.
function generateDateList(from, to) {
var getDate = function(date) { //Mysql Format
var m = date.getMonth(), d = date.getDate();
return date.getFullYear() + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
}
var fs = from.split('-'), startDate = new Date(fs[0], fs[1], fs[2]), result = [getDate(startDate)], start = startDate.getTime(), ts, end;
if ( typeof to == 'undefined') {
end = new Date().getTime();
} else {
ts = to.split('-');
end = new Date(ts[0], ts[1], ts[2]).getTime();
}
while (start < end) {
start += 86400000;
startDate.setTime(start);
result.push(getDate(startDate));
}
return result;
}
console.log(generateDateList('2014-2-27', '2014-3-2'));
I test it from chrome and nodejs below are the result.
[ '2014-02-27',
'2014-02-28',
'2014-02-29',
'2014-02-30',
'2014-02-31',
'2014-03-01',
'2014-03-02' ]
yeh big leap year:-D..., how can i fix this? or is there any better way.?
const listDate = [];
const startDate ='2017-02-01';
const endDate = '2017-02-10';
const dateMove = new Date(startDate);
let strDate = startDate;
while (strDate < endDate) {
strDate = dateMove.toISOString().slice(0, 10);
listDate.push(strDate);
dateMove.setDate(dateMove.getDate() + 1);
};
Take the start date and increment it by one day until you reach the end date.
Note: MySQL dates are standard format, no need to parse it by hand just pass it to the Date constructor: new Date('2008-06-13').
const addDays = (date, days = 1) => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
const dateRange = (start, end, range = []) => {
if (start > end) return range;
const next = addDays(start, 1);
return dateRange(next, end, [...range, start]);
};
const range = dateRange(new Date("2014-02-27"), new Date("2014-03-02"));
console.log(range);
console.log(range.map(date => date.toISOString().slice(0, 10)))
Here I use a recursive function, but you could achieve the same thing using a while (see other answers).
I have used this one from
https://flaviocopes.com/how-to-get-days-between-dates-javascript/
const getDatesBetweenDates = (startDate, endDate) => {
let dates = []
//to avoid modifying the original date
const theDate = new Date(startDate)
while (theDate < new Date(endDate)) {
dates = [...dates, new Date(theDate)]
theDate.setDate(theDate.getDate() + 1)
}
dates = [...dates, new Date(endDate)]
return dates
}
Invoke the function as follows:
getDatesBetweenDates("2021-12-28", "2021-03-01")
Note - I just had to fix issues with the Date object creation (new Date()) in the while loop and in the dates array. Other than that the code is pretty much same as seen on the above link
dateRange(startDate, endDate) {
var start = startDate.split('-');
var end = endDate.split('-');
var startYear = parseInt(start[0]);
var endYear = parseInt(end[0]);
var dates = [];
for(var i = startYear; i <= endYear; i++) {
var endMonth = i != endYear ? 11 : parseInt(end[1]) - 1;
var startMon = i === startYear ? parseInt(start[1])-1 : 0;
for(var j = startMon; j <= endMonth; j = j > 12 ? j % 12 || 11 : j+1) {
var month = j+1;
var displayMonth = month < 10 ? '0'+month : month;
dates.push([i, displayMonth, '01'].join('-'));
}
}
return dates;
}
var oDate1 = oEvent.getParameter("from"),
oDate2 = oEvent.getParameter("to");
var aDates = [];
var currentDate = oDate1;
while (currentDate <= oDate2) {
aDates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
I expanded Công Thắng's great answer to return {years, months, days}, thought it was worth sharing:
function getDates(startDate, endDate) {
const days = [],
months = new Set(),
years = new Set()
const dateMove = new Date(startDate)
let date = startDate
while (date < endDate){
date = dateMove.toISOString().slice(0,10)
months.add(date.slice(0, 7))
years.add(date.slice(0, 4))
days.push(date)
dateMove.setDate(dateMove.getDate()+1) // increment day
}
return {years: [...years], months: [...months], days} // return arrays
}
console.log(getDates('2016-02-28', '2016-03-01')) // leap year
/* =>
{
years: [ '2016' ],
months: [ '2016-02', '2016-03' ],
days: [ '2016-02-28', '2016-02-29', '2016-03-01' ]
}
*/
const {months} = getDates('2016-02-28', '2016-03-01') // get only months
Basically the function just increments the built-in Date object by one day from start to end, while the Sets capture unique months and years.

How to loop through dates using for and while loops

I would like to be able to loop through an XML file to find the earliest and latest date. Once those dates are found, I want to loop through the records again and compare the month and year from each record to the date that is being passed in. If the month and year match, then the count is increased by one. After the loop goes through all of the records, the date and count are added to an array, the date being passed in is then increased by one, and the loops runs again. The while loop continues to run until the date being increased is greater than the latest record date.
MY CODE:
var record=xmlDoc.getElementsByTagName("record");
var maxDate = 0;
// Loop through all of the records to find the latest date
for(var i=0; i<record.length; i++)
{
var tempMaxDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
if(tempMaxDate > maxDate)
{
maxDate = tempMaxDate;
}
}
var minDate = 99999999999999;
// Loop through all of the records to find the earliest date
for(var i=0; i<record.length; i++)
{
var tempMinDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
if(tempMinDate < minDate)
{
minDate = tempMinDate;
}
}
var minDateGraph = minDate;
var count = 0;
var data = [];
// Loop until the minimum date is greater than the maximum
while(minDate <= maxDate)
{
var tempMinDate2 = new Date(0);
tempMinDate2.setMilliseconds(minDate);
minDate = tempMinDate2;
// Loop through all of the records
// If the month and year of minDate match the record then the count is increased
for(var i=0; i<record.length; i++)
{
var tempVisitDate = record[i].getElementsByTagName("visit_date")[0].childNodes[0].nodeValue;
var visitDate = new Date(0);
visitDate.setMilliseconds(tempVisitDate);
if((visitDate.getMonth() == minDate.getMonth()) && (visitDate.getFullYear() == minDate.getFullYear()))
{
count += 1;
}
}
var tempData = [minDate, count];
data.push(tempData);
var month = minDate.getMonth();
var year = minDate.getFullYear();
// Increase the minDate by one month
if(month == 12)
{
year += 1;
month = 1;
}
else
{
month += 1;
}
var tempMinDate3 = new Date(year, month, 1).getTime();
minDate = tempMinDate3;
}
The problem is when I run my current code, the browser crashes. I think I am running into an infinite loop, but I cannot find what is causing it. I would appreciate any suggestions. Thanks!

Categories

Resources