Not able to get the Month in the format as exepected. - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Shweta DimriShweta Dimri 

Not able to get the Month in the format as exepected.

Hello Trailblazers,
I am trying to get the Month from a date field using formula, but the format of the month is not coming as expected. For egs, I have a date field, as Term Start Date and the date format is dd/mm/yyyy. So, if my term start date is 01/02/2021, and I am trying to get the month in a formula field, it is showing the month as "2", but I am expecting it to show as "02". I have tried the formula with return types as both text and number, but the result  is showing up as "2" only. Any ideas, how I can get the exact month format.
User-added imageUser-added image
Best Answer chosen by Shweta Dimri
Eric PraudEric Praud
Hi Shweta,

You could go with a formula returning text:
LPAD(TEXT(MONTH(Term_Start_Date__c)),2,"0")
Or
IF(MONTH(Term_Start_Date__c)<10,"0",NULL)&TEXT(MONTH(Term_Start_Date__c))


 

All Answers

Eric PraudEric Praud
Hi Shweta,

You could go with a formula returning text:
LPAD(TEXT(MONTH(Term_Start_Date__c)),2,"0")
Or
IF(MONTH(Term_Start_Date__c)<10,"0",NULL)&TEXT(MONTH(Term_Start_Date__c))


 
This was selected as the best answer
Shweta DimriShweta Dimri
Thank you so much @Eric Praud, both the formula's worked nicely. Would you mind explaining me that, why it did not show the expected month format, even though it was in "the format"?
Eric PraudEric Praud
Salesforce always removes leading zeros, that's just the way it is. This is why you need a formula returning text so you can hard-code it.
The first formula uses LPAD (left padding). I'm basically asking the system to add 0s on the left of the result so it is at least 2 characters long.
The second formula simply checks if the month is less than 10, if so add "0", otherwise nothing, then concatenate the month number
Shweta DimriShweta Dimri
Thanks a ton @Eric Praud for the explanation!!!