Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add date to a PDF file name.

I am sending an email from apex and attaching one PDF file with it.

Now I want to set string with dynamic date with it.


date fromdate = date.newinstance((,i.startdate__c.month(),i.startdate__c.year());
date todate = date.newinstance((,i.startdate__c.month(),i.startdate__c.year()+1);

string formateddate1 = fromdate.format(MM d, yyyy);
string formateddate2 = todate.format(MM d, yyyy);

att.setfilename('my file' +formateddate1 'to' +formateddate2);

It's showing PDF name like - my file 0000-11-06-08 00 00 to 0000-12-06-08 00 00

it's showing time also and wrong date. How do i correct it? I want only date without time

share|improve this question

The Date class does not have a format method that takes a formatting string but the Datetime class does. You can create Datetime without having to set time parts and then format it:

Datetime dtFrom = Datetime.newInstance(
String formattedFrom = dtFrom.format('MM d, yyyy');

Note the ordering of the year/month/day arguments too. Also see format options.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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