0

I use this to get the current date with one month added to it:

var date = new Date();
day = date.getDate();
month = date.getMonth() + 1;
month = month + 1;
if((String(day)).length == 1)
    day = '0' + day;
if((String(month)).length == 1)
    month = '0' + month;

dateT = day+ '.' + month + '.' + date.getFullYear();
console.log("dateT: " + dateT);

Using console.log("dateT: " + dateT); I get 14.02.2020.

I have a method that adds an element to a list on SharePoint.

There I use var posDeliveryDate = dateT; and then oListItem.set_item('DeliveryDate', posDeliveryDate);, but I get the error that the String was not recognized as a valid datetime.

I've seen that there are lots of threads regarding this issue, but what do I have to change in my case?

I tried day = date.getDate().toDateString(); but that didn't help and I can't use DateTime.ParseExact() in JavaScript..

Even when I manually enter a date, such as "14.02.2020" instead of posDeliveryDate, I get the error.

2 weeks ago the code worked, but now suddenly I get the error

1

Use the below lines of code:

dateT =new Date(date.getFullYear(),month, day) ;
var posDeliveryDate = dateT;
oListItem.set_item('DeliveryDate', posDeliveryDate);

Now the dateT will be in the correct format to be inserted into SharePoint list.

For more information, refer below link:

Updating a list item using date() function.

| improve this answer | |
  • 1
    Again, you saved me hours of trial and error. Thanks a lot !! – Ramanujan Taylor Jan 14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.