Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

E.g....

date fromdate = date.newinstance((i.startdate__c.day(),i.startdate__c.month(),i.startdate__c.year());
date todate = date.newinstance((i.startdate__c.day(),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
add comment

1 Answer 1

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(
        i.startdate__c.year(),
        i.startdate__c.month(),
        i.startdate__c.day()
        );
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
add comment

Your Answer

 
discard

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.