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
ahadipanahahadipanah 

Remove Spaces, characters from CloseDate

Hello,

I'm new to the forums so apologizes if this post isn't in the correct spot.

 

I'm basically looking for a way to generate the ClosedDate as a custom field as either 7-14-2011-440PM or just 7142011440PM instead of 7/14/2011 4:40 PM. 

 

Any help would be much appreciated. Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

If you are doing it in Apex,

Do something like this:

 

DateTime Dt = Datetime.now();
system.debug(Dt);
// output - 2011-07-19 07:14:48
String myDate = Dt.format('MdyyyyHmm')+'PM';
system.debug(myDate);
//    output - 7192011014PM

 Hope it helps. :)

All Answers

Shashikant SharmaShashikant Sharma

If you want to use a formula field you can do it like this

 

Add a formula field of Text Return type

 

SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(CloseDate), '/', '-') , ' ' , '-'),':','-')

 If you want to write a trigger I can help you in  that also

Ankit AroraAnkit Arora

When we get the date field in apex then it is returned in this type of format

 

String str = '' + Date.Today() ;
System.debug('::::: ' + str) ;

2011-07-18 00:00:00

 Now if you want to remove the ":" and " " from the date and get it in string then you can do something like this :

 

String str = '' + Date.Today() ;
str = str.replace(':' , '') ;
str = str.replace(' ' , '') ;
System.debug('::::: ' + str) ;

So this will satisfy your first format "7-14-2011-440PM"


Now if you want to remove everything between the date then you can try this :

 

String str = '' + Date.Today() ;
str = str.replace(':' , '') ;
str = str.replace(' ' , '') ;
str = str.replace('-' , '') ;
System.debug('::::: ' + str) ;

 It will provide you the date in this format : "7142011440PM"


This is all about how you can do this in apex, if you want to do this in formula then you can use SUBSTITUTE as suggested by Shashikant.


SUBSTITUTE is similar to replace.  We can not use replace in formula.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Rahul SharmaRahul Sharma

If you are doing it in Apex,

Do something like this:

 

DateTime Dt = Datetime.now();
system.debug(Dt);
// output - 2011-07-19 07:14:48
String myDate = Dt.format('MdyyyyHmm')+'PM';
system.debug(myDate);
//    output - 7192011014PM

 Hope it helps. :)

This was selected as the best answer
ahadipanahahadipanah

Thanks all for the solutions! Much appreciated!