Hello stackexchange gurus.

I have a problem with setting a future Due Date for a task using javascript in Salesforce. Below is the piece of code i use:

var d = new Date();
    var date =(d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear()).dateFormat;
    taskToCreate.ActivityDate = date;

Unfortunately, the result of this code is empty "Due Date" field. In case i get rid of .dateFormat, the error will appear, saying something like "incorrect value format for required field".

In case there is a simplier solution for setting Due Date as "7 days from today's date", i'd be grateful if you share your knowledge.

I was unable to find the answer on my own, and i will really appreciate any advice on that issue.

Thank you in advance, Best Regards.

  • I'm not a specialist of javascript, but is it normal to have quote on the last line ? Jan 12, 2015 at 14:02
  • Hi Yann G. That was an old bit of code, the one i'm using in SF is without quotes (but still with the same result). Thanks fo noticing that, will change it in the main message.
    – Emilio
    Jan 12, 2015 at 14:05
  • Have you tried to format this date format : var date =d.getFullYear() + "-" + (d.getMonth()+1)+ "-" + d.getDate(); Jan 12, 2015 at 14:23
  • When i manually add a date to a field, the date separator is "/" (also thats how the date is presented in SF, e.g. 23/01/2015). I also tried to switch places of month/day (UK and US version), but that wasnt the case, got the same result..
    – Emilio
    Jan 12, 2015 at 14:45
  • 1
    check out the solution for this question salesforce.stackexchange.com/questions/17644/….. You can use sforce.internal.dateTimeToString(new Date()) function to the work done.
    – Sam
    Jan 12, 2015 at 15:10

2 Answers 2


Managed to solve the issue by changing the code to this:

var d = new Date();
var date1 =((d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear());
taskToCreate.ActivityDate = new Date(date1);

Thanks everybody who spent their time to go through the question!


Thanks for the Q&A. I needed to assign these dates to custom fields so here is my solution. Now these variables are eligible to be used in component.set()

    var d = new Date();
    var today =$A.localizationService.formatDate(new Date(), "YYYY-MM-DD");
    var nextweek = $A.localizationService.formatDate(new Date((d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear()), "YYYY-MM-DD");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .