[removed] output current datetime in YYYY/mm/dd hh:m:sec format

后端 未结 6 692
囚心锁ツ
囚心锁ツ 2020-11-30 08:21

I need to output the current UTC datetime as a string with the following format:
YYYY/mm/dd hh:m:sec

How do I achieve that with Javascript?

6条回答
  •  误落风尘
    2020-11-30 08:48

    Posting another script solution DateX (author) for anyone interested

    DateX does NOT wrap the original Date object, but instead offers an identical interface with additional methods to format, localise, parse, diff and validate dates easily. So one can just do new DateX(..) instead of new Date(..) or use the lib as date utilities or even as wrapper or replacement around Date class.

    The date format used is identical to php date format.

    c-like format is also supported (although not fully)

    for the example posted (YYYY/mm/dd hh:m:sec) the format to use would be Y/m/d H:i:s eg

    var formatted_date = new DateX().format('Y/m/d H:i:s');
    

    or

    var formatted_now_date_gmt = new DateX(DateX.UTC()).format('Y/m/d H:i:s');
    

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

提交回复
热议问题