addDays function

Hello,

How do I automatically have a date field populate based on another date field +2 days? I want to addDays to another field. Thank you!

Hello @ParAvion,

You can add days to the date with the help of ready-made solution Moment.js

To use it, download moment-with-locales.js file from and upload them in the SharePoint Document Library.

Below is the code sample to add two days to the selected date. Please replace 'Date1' and 'Date2' with the internal names of the fields.

window.$ = $;
window.define = null;


fd.spRendered(function() {
	$.getScript('{LibraryURL}/moment-with-locales.js')
	.then(function() { return $.getScript('{LibraryURL}/moment-with-locales.js')})
	.then(function() {
		var m = moment();
		var date = moment(fd.field('Date1').value);
		var newDate = date.add(2, 'days').calendar();
		fd.field('Date2').value = newDate;
	})
});
1 Like

@mnikitina thanks for the response. Unfortunately I haven't been able to get this working. Here is the code i'm using:

@ParAvion,

Which Sharepoint version are you using: Online or On-premises?

Do you get any errors in the console (F12)? Could you please share the screenshot.

@mnikitina

I'm using Online. Console error:

image

@ParAvion,

The error indicates the syntax error in the script.

Is that the complete code that you have in the form? Where did you use the code?

Please try to use only the below code in JavaScript Editor and check if you still have any errors in the console.

image

window.$ = $;
window.define = null;


fd.spRendered(function() {
	$.getScript('{LibraryURL}/moment-with-locales.js')
	.then(function() {
		var m = moment();
		var date = moment(fd.field('Date1').value);
		var newDate = date.add(2, 'days').calendar();
		fd.field('Date2').value = newDate;
	})
});

@mnikitina

Thanks for helping, I've attached the complete code.

JS Code.txt (3.3 KB)

@ParAvion,

The closing characters of the fd.spRendered function were missing. Please see the updated code attached.

JS Code_Updated.txt (3.3 KB)

Please find more details about how to add days to date in the Manipulate Date and DateTime fields article.