To get the month starting from a capital letter when using toLocaleDateString()
with a specific locale in JavaScript, you can use the following code:
index.tsx218 chars6 lines
Explanation:
locale
variable with the desired locale for formatting the date string. In this case, we set it to fr-fr
for French (France).Date
object to represent the current date and time.toLocaleDateString()
method of the Date
object to obtain the full name of the month in the desired locale. We pass the { month: 'long' }
option to specify that we want the full month name.replace()
method with a regular expression to replace the first character of the month name with its uppercase counterpart. The str => str.toUpperCase()
function is used as the replacement value because it returns the uppercase version of the first character of the string.With these steps, we get the month with the first letter capitalized, as expected.
gistlibby LogSnag