I have a problem where i need to iterate been two different numbers or years. So for example. My start year is 1993 and my end year is 2014. How do i print out the year for year in between starting at 1993 and ending at 2014? Also for each year printed i need to add it to an array.
My assumption was to use a While loop like
var myYear = [];
var theYear;
while(startyear <= endYear){
myYear.push(startyear)
startyear++
}
You need to declare and initialize your variables.
Like this:
var startYear = 1993;
var endYear = 2014;
var yearArray = [];
while (startYear <= endYear) {
yearArray.push(startYear);
startYear++;
}
Why not use a for loop?
var year,
myYear = [];
for (year = startyear; year <= endYear; year ++){
myYear.push(year)
}
Related
In below code I can generate array of years but it's not dynamic. Year should start from 1901 to current year and in reverse order.
const theDates = [];
let cell = 1900;
for (let x = 0; x < 24; x += 1) {
const row = [];
for (let y = 0; y < 5; y += 1) {
row.push(cell += 1);
}
theDates.push(row.reverse());
}
console.log(JSON.stringify(theDates.reverse()).toString());
Currently, I am generating years till 2020 but I am hard-coding it. I need something like below, say that this is year 2023
[[2023,2022,2021,2020,2019], ... [1903,1902,1901,null,null]]
At any given point in time there should be 5 items in the inner array and last empty array values can be null
thanks in advance :)
Get the year with new Date().getFullYear(), then you can push chunks of 5 while the year number is greater than 1901:
const theDates = [];
for (let year = new Date().getFullYear(); year !== null;) {
const row = [];
for (let i = 0; i < 5; i++, year = (year > 1901 ? year - 1 : null)) {
row.push(year);
}
theDates.push(row);
}
console.log(theDates);
Example of year other than 2020:
const theDates = [];
for (let year = 2023; year !== null;) {
const row = [];
for (let i = 0; i < 5; i++, year = (year > 1901 ? year - 1 : null)) {
row.push(year);
}
theDates.push(row);
}
console.log(theDates);
I haven't tested it, but according to google you can do something like
var today = new Date();
var year = today.getFullYear();
since getFullYear() – Provides current year.
I am trying to create recurrence every 3 and 6 months using the later.js(https://github.com/bunkat/later).
This is my code
// My value.scheduled_date is 2018-09-06
var d = new Date(value.scheduled_date);
var day = d.getDate();
// month count will be -1 as it starts from 0
var month = d.getMonth();
var year = d.getFullYear();
var recurSched = later.parse.recur().on(day).dayOfMonth().every(3).month();
var schedules = later.schedule(recurSched).next(5);
console.log(schedules)
This gives 3 months recurrence starting from the current month, but I want the recurrence to start from the scheduled_date. When I add starting On to the code
var recurSched = later.parse.recur().on(day).dayOfMonth().every(3).month().startingOn(month + 1);
var schedules = later.schedule(recurSched).next(5);
console.log(schedules)
Recurrence is staring only after that month for every year. I want recurrence to start from a particular date so that the recurrence of other years won't be affected.
The same is the case with my 6 months code
var recurSched = later.parse.recur().on(day).dayOfMonth().every(6).month().startingOn(month+1);
var schedules = later.schedule(recurSched).next(5);
console.log(schedules);
I initially figured out the months in a year which can have reminders and later created the cron for the number of years required. For example; if we are creating a quarterly reminder on Jan 01,2018 it will repeat on Apr, July, Oct and Jan. Code for the above is given below
var recurSched = "";
var next_scheduled_date = "";
var next_scheduled_day = "";
var next_scheduled_month = "";
var next_scheduled_year = "";
var list_of_months = [];
for (var i = 1; i <= 2; i++) {
var next_scheduled_date = new Date(value.scheduled_date);
next_scheduled_date = new Date(next_scheduled_date.setMonth(next_scheduled_date.getMonth() + parseInt(i*6)));
var next_scheduled_day = next_scheduled_date.getDate();
var next_scheduled_month = next_scheduled_date.getMonth();
var next_scheduled_year = next_scheduled_date.getFullYear();
list_of_months.push(parseInt(next_scheduled_month + 1))
};
list_of_months.sort(function(a, b){return a - b});
Please note we also need to sort the months in the ascending order. The code for the recursion is as follows
var recurSched = later.parse.recur().on(list_of_months[0],list_of_months[1]).month().on(day).dayOfMonth();
var schedules = later.schedule(recurSched).next(4);
If you want to run recurring function independently and efficiently, better use your system's scheduler i.e. Linux has crontab
You can refer crontab here. Also, you can refer to crontab guru for multiple patterns. Example:
0 9 * 3,6,9,12 * node script.js
This will run your script.js at 09:00 in March, June, September, and December.”
I am trying to get the years included within the given date range.
Such that a user and input Dec. 1st 2015 - July. 1st 2016. I can get 2015 but I need to include 2016 as well.
Currently I am doing the difference between the two dates as years:
var tmpStart = moment($scope.dates1.startDate);
var tmpEnd = moment($scope.dates1.endDate);
var years = tmpEnd.diff(tmpStart, 'year');
for (var i = 0; i < years+1; i++) {
var tmpD = moment($scope.dates1.startDate);
tmpD.add(i, 'years');
sc.dtColumns.push(DTColumnBuilder.newColumn(tmpD.format('YYYY')).withTitle('Total ' + tmpD.format('YYYY')));
}
What is the best way to do this? I am sure I can just parse the year as int and compare to see how many years there are but I feel like that cannot be the proper way.
$scope.startDate = '01/01/2014';
$scope.endDate = moment(d).format("MM/DD/YYYY");
var tmpEndYear = moment(d).format("YYYY");
var tmpStart = moment("20140101", "YYYYMMDD");
sc.dtColumns = [DTColumnBuilder.newColumn('ProductNumber').withTitle('Item')];
var tmpEnd = moment(d);
$scope.dates1.startDate = moment("20140101", "YYYYMMDD");
$scope.dates1.endDate = tmpEnd;
for (var i = 2014; i <= tmpEndYear; i++) {
sc.dtColumns.push(DTColumnBuilder.newColumn(i).withTitle('Total ' + i));
}
while (tmpEnd > tmpStart) {
sc.months.push(tmpStart.format('MM-YYYY'));
tmpStart.add(1, 'month');
}
Did eventually parse the int out and use it from there.
Hopefully someone can help.
I have the following function to return the last 6 monthly names (I.E June, July, August etc), however, I cannot work out how I would then use each returned month name as separate variables.
I need this so I can feed each month name in to an sql query that populates a table for the last 6 months.
Any help much appreciated.
function getMonths()
{
var today = new Date();
var month = 1;
var currMonth = month-3;
var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");
var menuMonths = new Array();
var count = 6;
var buffer = 10;
while(count >0)
{
if (currMonth < 0)
currMonth += 12;
if (currMonth >=12 )
currMonth -= 12;
var month = monthArray[currMonth];
menuMonths.push(month);
currMonth = currMonth -1;
count = count -1;
}
return (menuMonths.reverse());
}
console.log (getMonths());
You can use Array.slice()
FIDDLE http://jsfiddle.net/BeNdErR/NF5Qm/1/
CODE
var monthArray = new Array("January","February","March","April","May","June",
"July","August","September","October","November","December");
var currMonth = 3; // the current month index, from 1 to 12
var firstMonth = currMonth - 6;
if(firstMonth < 0){ //for example if currMonth is January - 0
var months = [];
months.push(monthArray.slice(12 - Math.abs(firstMonth), 12));
months.push(monthArray.slice(0, currMonth));
alert(months);
}else{
alert(monthArray.slice(firstMonth, currMonth))
}
As output, you still have an array, so you can pass it to the SQL Query as it is, for example (SQLite):
tx.executeSql("SELECT * FROM tablename WHERE month = ? OR month = ? month = ? OR month = ? month = ? OR month = ?;", [slicedMonthArray], successCB, errorCB);
Hope it helps
var months = getMonths();
for(var i=0, len=months.length; i<len; i++){
var currentMonth = months[i];
alert(currentMonth);
//do whatever you want here;
}
I would like to have array in numbering for days and months from starting day 1 or month 1 till todate.
For example:
Today is 05-Oct-2012
Array for days: Output (1,2,3,4,5)
Array for months: Output (1,2,3,4,5,6,7,8,9,10)
Those array will be use in axis-x for charts.
You can use the Date object together with this useful Array.range() implementation to get the arrays;
var date = new Date("05-Oct-2012");
var days = Array.range(1, date.getDate());
console.log(days);
var months = Array.range(1, date.getMonth() + 1); // Zero-based, so add one.
console.log(months);
Full jsfiddle here.
try this ,
PHP
<?php
$date= date('Y-m-d');
$dayarray=range(1,date('d',strtotime($date)));
$monthharray=range(1,date('m',strtotime($date)));
JAVASCRIPT
var now= new Date();
var daysArray = [];
for (var i=1; i<=now.getDate(); i++)
daysArray.push(i);
var monthsArray = [];
for (var i=1; i<=now.getMonth()+1; i++)
monthsArray.push(i);
Use the methods of the Date objects:
var today = new Date();
var days = [];
for (var i=1; i<=today.getDate(); i++)
days.push(i);
var months = [];
for (var i=1; i<=today.getMonth()+1; i++)
months.push(i);