+ Start a Discussion
balraj singh 33balraj singh 33 

The date format is not coming correctly

 date formating using Outputtext is not displayinfg the correct date 
it is converting Mon Dec 30 00:00:00 GMT 2019
to 12/30/2020 whereas it should be 12/30/2019

Using the below expression 
<apex:outputText value="{0,date,MM/dd/YYYY}"> <apex:param value="{!mydate.CreatedDate}" /> </apex:outputText>
Best Answer chosen by balraj singh 33
ShirishaShirisha (Salesforce Developers) 
This should not be the expected behavior.

I have tried the below sample code and it is working as expected.

<apex:page > <apex:outputText value="{0,date,MM/dd/yy}"> <apex:param value="{!NOW()}" /> </apex:outputText> </apex:page>

Can you also please try this to see,if it is showing as expected or not.Also,try by changing the value on the {!mydate.CreatedDate} field to get the recent data or re-save the page to troubleshoot the issue.

Thank you!

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Balraj,

Greetings!

If you would like to display in the "Mon Dec 30 00:00:00 GMT 2019" format Then you can use the below syntax:

<apex:outputText value="{0, date, MMMM d',' yyyy}">

Otherwise,if you would like to display in the 12/30/2020 then the format that you are using should work:

 <apex:outputText value="{0,date,MM/dd/yy}"> 

As per your requirement,adjust the value on the outputText Tag.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

 
balraj singh 33balraj singh 33
@Shirisha  
i need covert it to 12/30/2019  but it is showing 12/30/2020
Mon Dec 30 00:00:00 GMT 2019  is my param value and 
<apex:outputText value="{0,date,MM/dd/YYYY}"> <apex:param value="{!mydate.CreatedDate}" /> </apex:outputText>
is the code i am using to covert it to 12/30/2019  but it is showing 12/30/2020
ShirishaShirisha (Salesforce Developers) 
Hey Balraj,

In that case,this is not the format issue.It is actually trying to display the value which was there on the {!mydate.CreatedDate}.If you would like to display the date by decreasing it 2019 from 2020 then you would need to write the formula.

Otherwise,change the value on {!mydate.CreatedDate} from 12/30/2020 to 
12/30/2019 which will resolve the issue.But,the format will not be able to show it as per your requirement unless you use any formulas to get it.

Check the below document for formulas on the date field:

https://help.salesforce.com/articleView?id=formula_using_date_datetime.htm&type=5

In your case,the date format that you have defined is absolutely working fine.

Thank you!
balraj singh 33balraj singh 33

Shirisha 
value on {!mydate.CreatedDate} is Mon Dec 30 00:00:00 GMT 2019

output expected : 12/30/2019
but 
output is : 12/30/2020
ShirishaShirisha (Salesforce Developers) 
This should not be the expected behavior.

I have tried the below sample code and it is working as expected.

<apex:page > <apex:outputText value="{0,date,MM/dd/yy}"> <apex:param value="{!NOW()}" /> </apex:outputText> </apex:page>

Can you also please try this to see,if it is showing as expected or not.Also,try by changing the value on the {!mydate.CreatedDate} field to get the recent data or re-save the page to troubleshoot the issue.

Thank you!
This was selected as the best answer
balraj singh 33balraj singh 33
@Shirisha
Issue was with date format , changed 'YYYY' to 'yy' it worked