Cannot read property 'length' error in JS - javascript

I have the an array of date range and I need to get a difference between number of days in between those months and create an array.
10/05/2015 - 11/05/2015 = 30 days
11/05/2015 - 12/ 05/2015 = 31 days
[30,31....]
I have the following code for date range.
function createLedger(stDate, etDate) {
if (stDate && etDate) {
var endOfLeaseDate = moment(etDate, "MM/DD/YYYY");
var startOfLeaseDate = moment(stDate, "MM/DD/YYYY");
dateRange(startOfLeaseDate, endOfLeaseDate);
}
}
function dateRange(stDate, etDate) {
var dates = [];
var now = stDate.clone();
var day = stDate.date();
while (now.isBefore(etDate)) {
//deal with variable month end days
var monthEnd = now.clone().endOf("month");
if (now.date() < day && day <= monthEnd.date()) {
now.date(day);
}
dates.push(now.format("MM/DD/YYYY"));
now = now.clone().add({
"months": 1
});
}
return dates;
}
function daysBetween(date1, date2) {
var Diff = Math.abs(date2.getTime() - date1.getTime());
var TimeDifference = Math.round(Diff / (1000 * 3600 * 24));
return TimeDifference;
}
function RunLedgerAndPV() {
var pDate = "11/21/2018"
var stDate = "10/5/2015";
var etDate = "12/4/2019";
var dateArr = createLedger(stDate, etDate);
var dayCounts = "";
for (var x = 0; x < dateArr.length; x++) {
dayCounts += daysBetween(dateArr[x], dateArr[x + 1], ",");
}
console.log(dayCounts);
}
RunLedgerAndPV();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
It's throwing an error at dateArr.length for some reason. Not sure what i am doing wrong here. Any help is appreciated. Thank you.

To add on to Vijay you are not returning anything in dateRange() either. Return dates array then return where you called dateRange().
Be aware this leads to more errors to do with your naming of daysBetween function when you are calling daysBetweenArrears.
EDIT
Few other errors:
You are calling getTime() on a string so this causes an error. You need to convert to a date object using new Date(date2) - new Date(date1).
Another return also missing for the Run function.
Code below:
function createLedger(stDate, etDate) {
if (stDate && etDate) {
var endOfLeaseDate = moment(etDate, "MM/DD/YYYY");
var startOfLeaseDate = moment(stDate, "MM/DD/YYYY");
return dateRange(startOfLeaseDate, endOfLeaseDate); // Added return
}
}
function dateRange(stDate, etDate) {
var dates = [];
var now = stDate.clone();
var day = stDate.date();
while (now.isBefore(etDate)) {
//deal with variable month end days
var monthEnd = now.clone().endOf("month");
if (now.date() < day && day <= monthEnd.date()) {
now.date(day);
}
dates.push(now.format("MM/DD/YYYY"));
now = now.clone().add({
"months": 1
});
}
return dates; // Added return
}
function daysBetween(date1, date2) {
var Diff = Math.abs(new Date(date2).getTime() - new Date(date1).getTime()); // Used new Date()
var TimeDifference = Math.round(Diff / (1000 * 3600 * 24));
return TimeDifference;
}
function RunLedgerAndPV() {
var pDate = "11/21/2018"
var stDate = "10/5/2015";
var etDate = "12/4/2019";
var dateArr = createLedger(stDate, etDate);
var dayCounts = "";
for (var x = 0; x < dateArr.length; x++) {
dayCounts += daysBetween(dateArr[x], dateArr[x + 1]) + ' '; // added a + ' ' to add a space to the result. Removed the ',' that you were adding in daysBetween but not using
}
return dayCounts; // Added return
}
RunLedgerAndPV(); //This wont show anything so wrap it in a console.log to see it return what you need

In your function "crrateLedger" you don't return anyting and you are assigning that in "var dateArr" hence it is set to undefined by javascript and you are trying to access property length of dateArr which is undefined

Related

create array of array of datepairs that have gap of n days between them

Consider 2 dates, format will be MM/DD/YYYY
1st date = today
2nd date = 45 days from today
Note: Here, the 1st date and 2nd date are variable.
i.e. 1st date that is today can be tomorrow or any other date. 2nd date can be 15 days, 24 days, 105 days i.e. this "n" can also vary.
Assuming the above 2 dates as startDate and stopDate. I want to create array of datePairs of a given gap between them.
For e.g. if startDate = 12/01/2022 & stopDate = 12/20/2022. I want to have datePairs having gap of 2 (n = 2) days between them. So, the output array should look like
[
['12/01/2022', '12/03/2022'],
['12/04/2022', '12/06/2022'],
['12/07/2022', '12/09/2022'],
['12/10/2022', '12/12/2022'],
['12/13/2022', '12/15/2022'],
['12/16/2022', '12/18/2022'],
['12/19/2022', '12/20/2022']
]
NOTE: Here, the last array does not have the gap of 2 dates because it's just 1 day away from the stopDate. In such case, the last pair can have less gap between them.
The only condition is the above array length should always be even.
Date.prototype.addDays = function (days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
};
function splitInto(array, size, inplace) {
var output, i, group;
if (inplace) {
output = array;
for (i = 0; i < array.length; i++) {
group = array.splice(i, size);
output.splice(i, 0, group);
}
} else {
output = [];
for (i = 0; i < array.length; i += size) {
output.push(array.slice(i, size + i));
}
}
return output;
}
function getDates(startDate, stopDate) {
var dateArray = new Array();
var currentDate = startDate;
var i = 0;
while (currentDate <= stopDate) {
if (i % 2 == 1) {
const options = {
year: 'numeric'
};
options.month = options.day = '2-digit';
var formattedCSTDate = new Intl.DateTimeFormat([], options).format(currentDate);
dateArray.push(formattedCSTDate);
currentDate = currentDate.addDays(1);
} else {
const options = {
year: 'numeric'
};
options.month = options.day = '2-digit';
var formattedCSTDate = new Intl.DateTimeFormat([], options).format(currentDate);
dateArray.push(formattedCSTDate);
currentDate = currentDate.addDays(3);
}
i = i + 1;
}
return dateArray;
};
var dateArray = getDates(new Date(), (new Date()).addDays(43));
var datePairLength = 2;
var rangeArray = splitInto(dateArray, datePairLength, false);
console.log(rangeArray);
It seems to me you're making it more complicated than it needs to be. Just build each range as an array and avoid the splitInto function. You might use a date library (there are many to chose from) for adding days and formatting:
function makeRanges(start = new Date(), end = new Date(), interval = 1) {
let f = new Intl.DateTimeFormat('default', {
year:'numeric',month:'short',day:'2-digit'
});
let s = new Date(start);
let ranges = [];
while (s < end) {
let t = new Date(s);
t.setDate(t.getDate() + interval);
ranges.push([f.format(s), t < end? f.format(t) : f.format(end)]);
s.setDate(s.getDate() + interval + 1)
}
return ranges;
}
console.log(
makeRanges(new Date(2022,0,1), new Date(2022,1,1), 2)
);

How to get the number of days with dates of a given week number by using JavaScript

I want to show them in a loop
Having an understanding of JavaScript & jQuery.
I have done to get the week number in a loop by using this code, for now, I want to get all days with dates of the given week number
function printWeekNumber() {
var dateFrom =
document.getElementById("txtFrom").value;
var dateF = new Date(dateFrom);
var resultFrom = dateF.getWeekNumber();
Date.prototype.getWeekNumber = function () {
var oneJan =
new Date(this.getFullYear(), 0, 1);
// calculating number of days
//in given year before given date
var numberOfDays =
Math.floor((this - oneJan) / (24 * 60 * 60 * 1000));
// adding 1 since this.getDay()
//returns value starting from 0
return Math.ceil((this.getDay() + 1 + numberOfDays) / 7);
}
function printWeekNumber() {
var dateFrom =
document.getElementById("txtFrom").value;
var dateF = new Date(dateFrom);
var resultFrom = dateF.getWeekNumber();
var dateTo =
document.getElementById("txtTo").value;
var dateT = new Date(dateTo);
var resultTo = dateT.getWeekNumber();
for (var i = resultFrom; i <= resultTo; i++) {
alert(i);
}
}
}
Please help me I am stuck in this step.

get last 7 days when user picks up a date

I have a datetimepicker where the user picks up a date, and my requirement is I need 7 days difference between his selected date.
For eg,
if user has selected 2017-03-01 so i need last 7 days from 2017-03-01 and NOT the current date
All answers i checked here were based on days difference from today.
Can anyone help me out here ?
$("#dateTimePickerIdWhereUserSelectsHisDate").val() - (7 * 24 * 60 * 60 * 1000);
this was on one of the answers but didn't work.
How can I achieve this ?
Try This
SelectDateTime will give you selected date
604800000 is 7 days in miliseconds
prevDate will give you last 7 days Date
$("#startDate").on("dp.change", function(e) {
if (e.oldDate != null) {
if (e.date.format('D') != e.oldDate.format('D')) {
var selectDateTime = e.date["_d"].getTime();
var prevDateTImeMili = selectDateTime - 604800000;
var prevDate = msToDateTime(prevDateTImeMili)
$('#startDate').data("DateTimePicker").hide();
}
}
});
msToDateTime is a function which converts milliseconds to DateTime
function msToDateTime(s) {
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
if(s != null){
s = new Date(s);
// var d = new Date(s);
// var d = new Date(s.getTime()+s.getTimezoneOffset()*60*1000+timeConversionToMilliseconds(sessionStorage.getItem("accounttimezone").split('+')[1]+':00'))
var d = new Date(s.getTime()+(s.getTimezoneOffset()*60*1000)+ (330 *60*1000));
dformat = [ d.getFullYear(),
(d.getMonth()+1).padLeft(),
d.getDate().padLeft()].join('-')+
' ' +
[ d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
return dformat;
}else{
return " ";
}
}
function getNDaysBefore(dateString, numberOfDaysBefore) {
let startingDate = new Date(dateString).getTime();
let datesArray = [],
daysCounter = 0,
day = 1000 * 60 * 60 * 24;
while (daysCounter < numberOfDaysBefore + 1) {
let newDateBeforeStaring = startingDate - day * daysCounter;
datesArray.push(new Date(newDateBeforeStaring));
daysCounter++;
}
return datesArray;
}
var dateString = "2016-03-01";
alert(getNDaysBefore(dateString,7));
With that kind of a function you can get any N days before the given date as an array of Date objects

date error for date class google appscript

The problem that i am having here is that when i minus back to then end of the month, instead of going back to the 29 or 28 of last month the program starts to minus months instead of days. Bellow is my full code and below that is the output it produces in the google spread sheet.
function trying(){
var date = new Date();
var datechange = new Date();
var array = new Array(7);
for (var i = 0; i < 7; i++) {
array[i] = new Array(0);
}
for ( var i = 0; i < 7; i++){
days = i + 8
datechange.setDate(date.getDate() - days);
var tabName = Utilities.formatDate(datechange, 'MST', 'yyyy-MM-dd').toString();
array[i][0] = tabName;
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Want");
sheet.getRange("B2:B8").setValues(array);
}
This are the dates that are produced.
05/07/2012
04/07/2012
03/07/2012
02/07/2012
01/07/2012
30/06/2012
30/05/2012
You have to define datechange inside your loop, and not outside:
var date = new Date();
for ( var i = 0; i < 30; i++){
days = i + 8
var datechange = new Date();
datechange.setDate(date.getDate() - i);
console.log(datechange);
}
Date.getDate() returns the date (1-31) - so what you are doing is not correct.
Instead try this:
var ONE_DAY = 24 * 60 * 60 * 1000; //in milliseconds
for ( var i = 0; i < 7; i++){
days = i + 8
datechange.setDate(date.getTime() - (days * ONE_DAY));
var tabName = Utilities.formatDate(datechange, 'MST', 'yyyy-MM-dd').toString();
array[i][0] = tabName;
}
This is how JavaScript dates work. See here for full details.

setDate(day+1) returns wrong days

Why does this:
startDate.setDate(startDate + 1);
Generates this strange order (broken at the beginning of the next month):
7, 27, 28, 30, 30, **1, 4, 3, 4, 4,** 6, 7, 10, 9, 10, 10, 12,
Update (more code):
renderWeekFromMonday : function(date) {
var dayContainer = $('#day');
// clear div
dayContainer.empty();
// Render days
for (var i = 0; i <= 7; i++) {
// Get mondday day (1-31)
var day = date.getDate();
// Today
var t = new Date();
// Create dayobject for usage inside for loop
var d = new Date(date);
// Render 7 days (1 week)
for (var i = 0; i < 7; i++) {
// New day (+1)
d.setDate(day + i)
console.info(d.getDate());
// Create html
var span = $("<span>").addClass("calElement").attr("time", d.getTime())
var w = 25;
span.html("<span class=dayNumber>" + d.getDate() + "</span><br>" + this.dayNames[d.getDay()]).css("width",w);
//span.html("<span class=dayNumber>" + d.getDate() + "</span>");
// Append day
dayContainer.append(span);
}
}
},
Just a guess, put perhaps you're looking for:
startDate.setDate(startDate.getDate() + 1);
startDate + 1 doesn't make much sense if startDate is a Date object.
After seeing updated code: Your problem is very likely in the fact that you have two nested loops, both of which increment i. Use a different variable for one of the loops.
Your code looks fine - I'm using FF4 and the date calculations look correct. I've posted the code here for a live example: http://jsfiddle.net/EbNcr/2/
Is there a specific browser or date(s) that you're testing with to get the strange results?
(I wish I could submit this as a comment, but I don't think I have the reputation for that yet...)
Thanks for helping. I just had a simple copy&paste error (two loops). I put the final code into a simple jquery plugin (inspiered by some oder plugins)
var MILLIS_IN_DAY = 86400000;
var MILLIS_IN_WEEK = MILLIS_IN_DAY * 7
jQuery.fn.calendarPicker = function(options) {
// -------------------------- start default option values --------------------------
options.date = new Date();
options.dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
options.firstDayOfWeek = 1;
options.showNavigation = true;
// -------------------------- end default option values --------------------------
var calendar = {
changeDate : function(date) {
// calculate new start date
this.currentFirstDayOfWeek = this._firstDayOfWeek(date);
// render days
for (var i = 0; i < 7; i++) {
// create dayobject
var day = new Date(this.currentFirstDayOfWeek.getTime() + MILLIS_IN_DAY * i);
// render container
var span = $("<span>").addClass("calElement").attr("time", day.getTime())
// render day
span.html("<span class=dayNumber>" + day.getDate() + "</span><br>" + options.dayNames[day.getDay()]);
if (day.getYear() == date.getYear() && date.getMonth() == day.getMonth() && day.getDate() == date.getDate())
span.addClass("today");
if (day.getYear() == date.getYear() && day.getMonth() == date.getMonth() && day.getDate() == date.getDate())
span.addClass("selected");
theDiv.append(span);
// render navigation
if (i == 0 && options.showNavigation) {
var prevBtn = $("<span>").addClass("prev").html("prev");
var self = this;
prevBtn.bind('click', function() { self.prevWeek(); });
theDiv.prepend(prevBtn);
} else if (i == 6 && options.showNavigation) {
var nextBtn = $("<span>").addClass("next").html("next")
var self = this;
nextBtn.bind('click', function() { self.nextWeek(); });
theDiv.append(nextBtn);
}
}
},
/*
* Go to the previous week relative to the currently displayed week
*/
prevWeek : function() {
//minus more than 1 day to be sure we're in previous week - account for daylight savings or other anomolies
var newDate = new Date(this.currentFirstDayOfWeek.getTime() - (MILLIS_IN_WEEK / 6));
this._clearCalendar();
this.changeDate(newDate);
},
/*
* Go to the next week relative to the currently displayed week
*/
nextWeek : function() {
//add 8 days to be sure of being in prev week - allows for daylight savings or other anomolies
var newDate = new Date(this.currentFirstDayOfWeek.getTime() + MILLIS_IN_WEEK + (MILLIS_IN_WEEK / 7));
this._clearCalendar();
this.changeDate(newDate);
},
/*
* returns the date on the first millisecond of the week
*/
_firstDayOfWeek : function(date) {
var midnightCurrentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var adjustedDate = new Date(midnightCurrentDate);
adjustedDate.setDate(adjustedDate.getDate() - this._getAdjustedDayIndex(midnightCurrentDate));
return adjustedDate;
},
_clearCalendar : function() {
theDiv.empty();
},
/*
* gets the index of the current day adjusted based on options (e.g.firstDayofWeek)
*/
_getAdjustedDayIndex : function(date) {
var midnightCurrentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var currentDayOfStandardWeek = midnightCurrentDate.getDay();
var days = [0,1,2,3,4,5,6];
this._rotate(days, this.options.firstDayOfWeek);
return days[currentDayOfStandardWeek];
},
/*
* rotate an array by specified number of places.
*/
_rotate : function(a, p ) {
for (var l = a.length, p = (Math.abs(p) >= l && (p %= l),p < 0 && (p += l),p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p)) {
for (i = l; i > p; x = a[--i],a[i] = a[i - p],a[i - p] = x);
}
return a;
},
};
// Pass options
calendar.options = options;
// build the calendar on the first element in the set of matched elements.
var theDiv = this.eq(0);//$(this);
theDiv.addClass("calBox");
// empty the div
theDiv.empty();
// entry point
calendar.changeDate(options.date);
return calendar;
};

Categories