MomentJs Get Years - Not Difference - javascript

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.

Related

How to generate array of years till current year?

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.

Every 3 and 6 Months Recurrence on later.js stating from a particular date

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.”

Trying to get Month and year range using Moment

I am trying to calculate Month name and year using moment for a range.
for example if the start year and month is 2016-02, and end year is 2017-11,
I want Moment to create date in
Feb-16 , Mar-16 ..Jan-17, Nov-17
Can you provide some idea? This is what I ma trying to do:
const startYear = 2016;
const startMonth = 2;
const endYear = 2017;
const endMonth = 12;
var ind = startMonth;
var i=0;
while(i < 24){
if(ind == 13){
ind = 1;
startYear++;
}
header = moment(startYear + '-'+ ind, 'YYYY-M').format('MMM-YY');
i++;
ind++;
return header ;
}
The output should be like :
Feb-16 Mar-16 Apr-16 May-16......Jan-17....Nov-17
when momentjs a library, try to use the convenience methods that they have for date manipulation instead of just using it for parsing
var startDate = moment('2012-1');
var endDate = moment('2013-1');
var out = [startDate];
while (startDate.isBefore(endDate)) {
startDate = startDate.add(1, 'month');
out.push(startDate);
}

How can I generate dates of a certain Gregorian year to Hijri

I want to make an auto adaptation or generation of days of year, from Gregorian to Hijri.
I mean that you want to select or write the year as example:
select 2015:
Get all the days of 2015 in Gregorian and then its convert to Hijrim and present the list of hijri.
so you want to return to 2 list list1 gregoriad days list vs another list2 hijhri
I want this in JavaScript and using kendo-ui framework to view it.
Kendo UI only supports the Gregorian calendar. There don't seem to be plans to add any others.
You could use .NET to convert the date.
public string ConvertDateCalendar(DateTime DateConv, ECalenderTypes calendar, string DateLangCulture)
{
System.Globalization.DateTimeFormatInfo DTFormat;
DateLangCulture = DateLangCulture.ToLower();
/// We can't have the hijri date writen in English. We will get a runtime error
if (calendar == ECalenderTypes.Hijri && DateLangCulture.StartsWith("en-"))
{
DateLangCulture = "ar-sa";
}
/// Set the date time format to the given culture
DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;
/// Set the calendar property of the date time format to the given calendar
switch (calendar)
{
case ECalenderTypes.Hijri:
DTFormat.Calendar = new System.Globalization.HijriCalendar();
break;
case ECalenderTypes.Gregorian:
DTFormat.Calendar = new System.Globalization.GregorianCalendar();
break;
default:
return "";
}
/// We format the date structure to whatever we want
DTFormat.ShortDatePattern = "dd/MM/yyyy";
return (DateConv.Date.ToString("f", DTFormat));
}
And then:
ConvertDateCalendar("01/01/2015", ECalenderTypes.Gregorian, "en-US");
ConvertDateCalendar("01/01/2015", ECalenderTypes.Hijri, "en-US");
JavaScript
function gmod(n,m){
return ((n%m)+m)%m;
}
function getDate(adjust){
var today = new Date();
if(adjust) {
adjustmili = 1000*60*60*24 * adjust;
todaymili = today.getTime() + adjustmili;
today = new Date(todaymili);
}
day = today.getDate();
month = today.getMonth();
year = today.getFullYear();
m = month+1;
y = year;
if(m<3) {
y -= 1;
m += 12;
}
a = Math.floor(y/100.);
b = 2-a+Math.floor(a/4.);
if(y<1583) b = 0;
if(y==1582) {
if(m>10) b = -10;
if(m==10) {
b = 0;
if(day>4) b = -10;
}
}
jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;
b = 0;
if(jd>2299160){
a = Math.floor((jd-1867216.25)/36524.25);
b = 1+a-Math.floor(a/4.);
}
bb = jd+b+1524;
cc = Math.floor((bb-122.1)/365.25);
dd = Math.floor(365.25*cc);
ee = Math.floor((bb-dd)/30.6001);
day =(bb-dd)-Math.floor(30.6001*ee);
month = ee-1;
if(ee>13) {
cc += 1;
month = ee-13;
}
year = cc-4716;
wd = gmod(jd+1,7)+1;
iyear = 10631./30.;
epochastro = 1948084;
epochcivil = 1948085;
shift1 = 8.01/60.;
z = jd-epochastro;
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
j = Math.floor((z-shift1)/iyear);
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
id = z-Math.floor(29.5001*im-29);
var myRes = new Array(8);
myRes[0] = day; //calculated day (CE)
myRes[1] = month-1; //calculated month (CE)
myRes[2] = year; //calculated year (CE)
myRes[3] = jd-1; //julian day number
myRes[4] = wd-1; //weekday number
myRes[5] = id; //islamic date
myRes[6] = im-1; //islamic month
myRes[7] = iy; //islamic year
return myRes;
}
function writeHijriDate(adjustment) {
var wdNames = new Array("Ahad","Ithnin","Thulatha","Arbaa","Khams","Jumuah","Sabt");
var iMonthNames = new Array("Muharram","Safar","Rabi'ul Awwal","Rabi'ul Akhir", "Jumadal Ula","Jumadal Akhira","Rajab","Sha'ban", "Ramadan","Shawwal","Dhul Qa'ada","Dhul Hijja");
var iDate = getDate(adjustment);
var outputHijriDate = wdNames[iDate[4]] + ", " + iDate[5] + " " + iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputHijriDate;
}
Usage (converts current date):
writeHijriDate(1);
Hirji calendar is not supported with Kendo UI.
Thanks all
I found someone write a simple implementation for the Islamic calender(Hijri) in Javascript
http://xsoh.github.com/Hijri.js
its consist the convert from hijri to gregorian and Vice versaز
The conversion is very easy using Intl object (read more), as following:
a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)

How do i create a loop to iterate between 2 numbers(years)?

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

Categories

Resources