toLocaleDateString() is not returning dd/mm/yyyy format

后端 未结 3 1193
轻奢々
轻奢々 2020-12-17 16:28

Hi I have used toLocaleDateString() to display a date from a rss feed,t its not showing dd/mm/yyyy format in all browser, safari and

3条回答
  •  暖寄归人
    2020-12-17 17:02

    I was looking for an answer to this question, but above answers don't give a crips answer for converting date to dd/mm/yyyy using toLocaleDateString().

    As per docs toLocaleDateString() converts a date to a string with a language sensitive representation of the date portion. This method accepts two parameters dateObj.toLocaleDateString( [locales][, options]) described below :

    • locales: This parameter is an array of locale strings that contain one or more language or locale tags.Note that it is an optional parameter.If you want to use specific format of the language in your application then specify that language in the locales argument.Some parameters are:

      • en-US : US English uses month-day-year order i.e 07/17/2020
      • en-GB : British English uses day-month-year order i.e 17/07/2020
      • ko-KR : Korean uses year-month-day order i.e 2020. 07. 17.
    • options: It is also an optional parameter and contains properties that specify comparison options.Some properties are localeMatcher, timeZone, weekday, year, month, day, hour, minute, second etc.

    So using this here is how you can convert date to dd/mm/yyyy format:

    let dateFormat=new Date().toLocaleDateString('en-GB', {
    month: '2-digit',day: '2-digit',year: 'numeric'})
    console.log(dateFormat)

提交回复
热议问题