Formatting the date time with Javascript

后端 未结 12 2327
暗喜
暗喜 2020-11-27 07:36

I have a date/time string like 2012-01-13 04:37:20 but I want to convert it to dd-mm-yyyy hh:mm, how can i do this?

I am using the followin

12条回答
  •  时光取名叫无心
    2020-11-27 08:03

    I think it is best to use the Intl.DateTimeFormat class.

    The usage is fairly straightforward. You can not enter a pattern as you want to, but it will give you the results you want.

    Here is an example on how to use it:

    public formatDate(date : Date) : string{
        var options = {  year: 'numeric', month: 'short', day: 'numeric' };
        return new Intl.DateTimeFormat('de-DE', options).format(date);
    }
    

    If you really want to enter a DateTimeFormat string, it would be easy enough to write a function which parses the string using Regex, but I don't think it is needed.

    For further Information go here:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

提交回复
热议问题