I am using the formulae:

myDatetime = Datetime.now();
myDatetimeStr = myDatetime.format('dd/MM/YYYY');

enter image description here

But the values I get are next' year's date.

13:37:40:000 USER_DEBUG   "Close Date" : "29/12/2016",

How is that even possible that the : DateTime.now() is so wrong?

Debug logs are even more crazy :-/

14:19:22.746 (746983721)|VARIABLE_SCOPE_BEGIN|[163]|myDatetime|Datetime|false|false 14:19:22.747 (747018312)|VARIABLE_ASSIGNMENT|[163]|myDatetime|"2015-12-29T00:00:00.000Z" 14:19:22.747 (747028286)|STATEMENT_EXECUTE|[164] 14:19:22.747 (747037545)|HEAP_ALLOCATE|[164]|Bytes:10 14:19:22.747 (747137413)|HEAP_ALLOCATE|[164]|Bytes:10 14:19:22.747 (747169556)|VARIABLE_SCOPE_BEGIN|[164]|myDatetimeStr|String|false|false 14:19:22.747 (747188016)|VARIABLE_ASSIGNMENT|[164]|myDatetimeStr|"29/12/2016"

  • 1
    check if you are adding something like addyear() Dec 29, 2015 at 12:12
  • thanks, not adding anything, simple 2 liner code. added debug log output to question
    – Saariko
    Dec 29, 2015 at 12:21

1 Answer 1


Use small y instead of capital Y

Datetime myDatetime = Datetime.now();
String myDatetimeStr = myDatetime.format('dd/MM/yyyy');

IF you looked into this link


The capital Y merge field is the WEEK YEAR - which from the 27th December 2015 is IN 2016!

The lower case y merge field is the CALENDAR YEAR - which will be 2015 as expected.

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.