I need some help with switching the format from DD-MM-YYYY or MM-DD-YYYY to ISO Standard (YYYY-MM-DD)in JavaScript. I'm using this on a custom URL Button. This is what i'm doing, and It is working fine for US date formats but not working for EU dates. I get invalid date error.


var Lastdate = new  Date('{!ServiceContract.EndDate}'); 

How can I instantiate a Date when using a different locale?

  • Is this ServiceContract.EndDate is date field? – Santanu Boral May 1 '17 at 19:11
  • that is correct @SantanuBoral – naruto-sfdc May 1 '17 at 19:15
  • where exactly you are facing issue? inside the custom button or some where else? – Santanu Boral May 1 '17 at 19:21
  • It saves fine, but when I click the button that I created, then I get alert message. – naruto-sfdc May 1 '17 at 19:29
  • got the error replicated.. – Santanu Boral May 1 '17 at 20:50

Yes, it will give an issue due to locale.

So, better retrieve year, month and day portion from the date and create a Date instance based on those parameters.

It will solve locale related issues.


var month = '{!MONTH(ServiceContract.EndDate)}'; 
var year = '{!YEAR(ServiceContract.EndDate)}'; 
var day = '{!DAY(ServiceContract.EndDate)}'; 

var Lastdate = new Date (year, month, day); 

  • Thank you this works great! I dont know why i didnt think of this approach – naruto-sfdc May 1 '17 at 21:20

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.