Datetime formatting / customization in ejs

后端 未结 6 1908
清歌不尽
清歌不尽 2021-02-07 04:09

I show date by this in ejs

<%= new Date();%>

it give me result

Tue Feb 02 2016 16:02:24 GMT+0530 (IST)

6条回答
  •  滥情空心
    2021-02-07 05:04

    The problem with most of the answers on this page is that they will format the date using the server's locale, but you probably want them rendered in the user's locale. For example, if your server is in Virginia, your dates will be displayed in English in Eastern Time, but a user in another country will probably want them displayed differently.

    The following code snippet will render date using the user's locale. There's room for improvement, but it's a starting point:

    This takes the date on the server, converts it to an ISO-8601 string, and interpolates it into a little JavaScript snippet that runs client-side to produce the correct localized output.

    For more information on toLocaleString, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

提交回复
热议问题