function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
developer03developer03 

Formatting date

Hi Guys,

I need help formatting date as YYYYMMDD. For example, 21st Janurary 2008 the formatted date would be 20080121. How would I do this?

Thanks
micwamicwa
Where would you like to format the date like this? Just to display in a emailtemplate or where?
developer03developer03
This is used as part of Opportunity name so it needs to be exactly like this :(

This is the Javascript function I used to accomplish the same thing in S-Control.

Code:
/**
 Returns formatted date value according to the passed in pattern. Following are the valid patterns
     
 !mmmm = Long month (eg. January)
 !mmm = Short month (eg. Jan)
 !mm = Numeric date (eg. 07)
 !m = Numeric date (eg. 7)
 !dddd = Long day (eg. Monday)
 !ddd = Short day (eg. Mon)
 !dd = Numeric day (eg. 07)
 !d = Numeric day (eg. 7)
 !yyyy = Year (eg. 1999)
 !yy = Year (eg. 99)    
**/
function dateFormat(aDate, displayPat){   
 intMonth = aDate.getMonth();
 intDate = aDate.getDate();
 intDay = aDate.getDay();
 intYear = aDate.getFullYear();
     
 var months_long =  new Array ('January','February','March','April',
     'May','June','July','August','September','October','November','December')
 var months_short = new Array('Jan','Feb','Mar','Apr','May','Jun',
     'Jul','Aug','Sep','Oct','Nov','Dec')
 var days_long = new Array('Sunday','Monday','Tuesday','Wednesday',
  'Thursday','Friday','Saturday')
 var days_short = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat')
     
 var mmmm = months_long[intMonth]
 var mmm = months_short[intMonth]
 var mm = intMonth < 9—'0'+ (1 + intMonth) + '':(1+intMonth)+'';
 var m = 1+intMonth+'';
 var dddd = days_long[intDay];
 var ddd = days_short[intDay];
 var dd = intDate<10–'0'+intDate+'':intDate+'';
 var d = intDate+'';
 var yyyy = intYear;
     
 century = 0;
 while((intYear-century)>=100)
  century = century + 100;
     
 var yy = intYear - century
  if(yy<10)
 yy = '0' + yy + '';
     
 displayDate = new String(displayPat);
     
 displayDate = displayDate.replace(/!mmmm/i,mmmm);
 displayDate = displayDate.replace(/!mmm/i,mmm);
 displayDate = displayDate.replace(/!mm/i,mm);
 displayDate = displayDate.replace(/!m/i,m);
 displayDate = displayDate.replace(/!dddd/i,dddd);
 displayDate = displayDate.replace(/!ddd/i,ddd);
 displayDate = displayDate.replace(/!dd/i,dd);
     displayDate = displayDate.replace(/!d/i,d);
 displayDate = displayDate.replace(/!yyyy/i,yyyy);
 displayDate = displayDate.replace(/!yy/i,yy);
    
 return displayDate; 
}

 I tried converting it to Apex but I didn't get very far.

Please help!

developer03developer03
Anyone? Someone please help!