Date fuss - overwriting a date

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

Date fuss - overwriting a date

#1

Postby alexmcclune » Wed Aug 30, 2017 12:00 pm

Hi,

I have the following code which is designed to populate an array with the next n dates for a specific day of the week. Unfortunately it is only ever using the same date and I am unsure why. Could you advise as to how I could overwrite the variable nextFriday each iteration:

Code: Select all

	var date = new Date();
	var day = date.getDay();
	var normalizedDay = (day + 2) % 7;
	var daysForward = 7 - normalizedDay;
	var nextFriday = new Date(+date + (daysForward * 24 * 60 * 60 * 1000));
At this point the nextFriday variable holds the next friday - the above worked. I now want to push this into an array and then push a further 8 values into the array, each of these values should be the next friday after the last leaving an array with the next 8 Friday dates in...

Code: Select all

	
  var availableDates = [];
  availableDates.push(nextFriday);
  
  var i = 0;
  while &#40;i < 8&#41;&#123;
		nextFriday.setDate&#40;nextFriday.getDate&#40;&#41; + 7&#41;;
		availableDates.push&#40;nextFriday&#41;;
		i++
	&#125;
I am certain the nextFriday.setDate(nextFriday.getDate() + 7);; is working as expected but how can I replace the value of nextFriday. If i use the syntax nextFriday = nextFriday.setDate(nextFriday.getDate() + 7); it doesn't work as the setDate function isn't valid (ebase error).

Alex
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Wed Aug 30, 2017 3:52 pm

Alex,

I think what is wrong is that nextFriday is just a single Date object that you end up changing many times. All entries in your array will point to this single object which will contain the final date. You should create a new date object for each pass through the loop e.g.

Code: Select all

var i = 0;
var lastDate = new Date&#40;nextFriday.getTime&#40;&#41;&#41;;
while &#40;i < 8&#41;
&#123;
	var d = new Date&#40;&#41;;
  d.setDate&#40;lastDate.getDate&#40;&#41; + 7&#41;;
  availableDates.push&#40;d&#41;;
  lastDate.setDate&#40;d.getDate&#40;&#41;&#41;;
  i++;
&#125;
Jon
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#3

Postby alexmcclune » Wed Aug 30, 2017 3:54 pm

Thanks Jon,

I actually resolved it in a similar same fashion:

Code: Select all

var nextFriday = new Date&#40;+date + &#40;daysForward * 24 * 60 * 60 * 1000&#41;&#41;;
fields.DATETEST.value = nextFriday;
	
var availableDates = &#91;&#93;;
availableDates.push&#40;fields.DATETEST.value&#41;;
  
  var i = 0;
  while &#40;i < 8&#41;&#123;
		fields.DATETEST.value = nextFriday.setDate&#40;nextFriday.getDate&#40;&#41; + 7&#41;;
		availableDates.push&#40;fields.DATETEST.value&#41;;
		i++
	&#125;
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#4

Postby Segi » Wed Aug 30, 2017 6:57 pm

If you write a lot of little standalone JavaScript snippets like this, I find that its much faster to use a live JavaScript site like www.jsfiddle.net to test it out first. Once its working, you can just paste the code directly into designer.

JSFiddle will also let you use external JS libraries like jQuery, React, Angular, Vue (for non Ebase projects)
0 x

alexmcclune
Ebase User
Posts: 95
Joined: Wed Feb 27, 2013 5:16 pm

#5

Postby alexmcclune » Thu Aug 31, 2017 7:43 am

Segi wrote:If you write a lot of little standalone JavaScript snippets like this, I find that its much faster to use a live JavaScript site like www.jsfiddle.net to test it out first. Once its working, you can just paste the code directly into designer.

JSFiddle will also let you use external JS libraries like jQuery, React, Angular, Vue (for non Ebase projects)
Yeah - I was actually trying out moment.js to save some effort with this yesterday.
0 x


Who is online

Users browsing this forum: No registered users and 17 guests