A is the correct and please refer the bellow discussion
https://salesforce.stackexchange.com/questions/154024/display-string-datetime-in-current-user-locale
A considers user locale.
I see some C answers here but it's not correct. String.valueOf(Date) converts a Date to a String, but without considering the user's locale; it is a fixed format.
the correct answer is A. A and C are very close but differ in the following way.
A is the date format method- it returns the date as string using the user locale.
C is a string method- it returns a string of the date specified in the standard format of YYYY-MM-DD. my locale is english(canada) the format is YYYY-MM-DD so seems that C is correct ans. But here is the catch. when I change my local to French(France) the date format of my local becomes DD/MM/YYYY. In this case String method will not work. Thus A is the ans.
valueOf(datetimeToConvert)
Returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the local time zone.
the correct answer is A. A and C are very close but differ in the following way.
A is the date format method- it returns the date as string using the user locale.
C is a string method- it returns a string of the date specified in the standard format of YYYY-MM-DD. my locale is english(canada) the format is YYYY-MM-DD so seems that C is correct ans. But here is the catch. when I change my local to French(France) the date format of my local becomes DD/MM/YYYY. In this case String method will not work. Thus A is the ans.
Date.format() returns the Date as a string using the locale of the context user
String.valueOf() returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the local time zone.
Ans is A
format() - Returns the Date as a string using the locale of the context user
// In American-English locale
date myDate = date.newInstance(2001, 3, 21);
String dayString = myDate.format();
system.assertEquals('3/21/2001', dayString);
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_date.htm#apex_System_Date_format
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Shah743
Highly Voted 2 years, 4 months agosinghkar
Highly Voted 2 years, 4 months agoBrainMelt12
Most Recent 1 month, 1 week agosanto_aj
5 months, 2 weeks agosanto_aj
5 months, 3 weeks agoopqrst
6 months, 3 weeks agoTing21
7 months agokarappo
8 months, 1 week agoram12313
6 months, 3 weeks agothneeb
8 months, 2 weeks agosf2022
10 months, 2 weeks agovm1406
11 months, 3 weeks agoCaiXiDan
1 year, 3 months ago