I have '273' added as a day with the following line:

DateTime.newInstance(dStart.year(), dstart.month(), dstart.day()).format('DD-MM-YYYY');

so the output looks like '273-09-2019' - what gives?

I am trying to add a date to a SOQL string and a Date type returns '30-09-2019 00:00:00'. A few suggestions were to convert Date to DateTime and then apply to format. Is there a better way of doing this?

1 Answer 1


It should be small dd aka dd-MM-YYYY

Date dStart = System.today();    

System.debug(DateTime.newInstance(dStart.year(), dstart.month(), dstart.day()).format('dd-MM-YYYY')); //13-11-2019

Format method follows SimpleDateFormat and according to it

Small d means date and Caps D means day of the year

  • 1
    As an addendum to this answer, Apex's date formatting method(s) accept format strings that adhere to Java's SimpleDateFormat. DD = day of the year (0 to 365 or 366), dd = day of the month (with leading 0).
    – Derek F
    Nov 13, 2019 at 17:06
  • Thanks @DerekF :) Nov 13, 2019 at 17:07
  • Cheers fellas! I can't believe how much time I wasted on this.
    – wmin
    Nov 13, 2019 at 17:22

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.