Formatting the date time with Javascript

后端 未结 12 2374
暗喜
暗喜 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:08

    Use either simple string manipulation (as suggested by @SKS) or use a library. The latter is more flexible and lets you change the input or output format easily. For example, using the Globalize.js library, you would write:

    var dd = Globalize.parseDate(now, "yyyy-MM-dd HH:mm:ss");
    dd = Globalize.format(dd, "dd-MM-yyyy HH:mm");
    

    Note however that formats such as "dd-mm-yyyy hh:mm" are confusing – it is neither a standard ISO format nor any localized (language-dependent) format. The Globalize.js library lets you use predefined language-dependent formats in addition to explicitly specified formats.

    Note that the built-in date and time parsing and formatting routines in JavaScript are implementation-dependent. Using them means non-portable code. For example, there is no guarantee that new Date() will accept the format you have as input, and toLocaleDateString() writes the date in some locale-dependent format, which can be just about anything.

提交回复
热议问题