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

后端 未结 6 695
囚心锁ツ
囚心锁ツ 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:49

    Not tested, but something like this:

    var now = new Date();
    var str = now.getUTCFullYear().toString() + "/" +
              (now.getUTCMonth() + 1).toString() +
              "/" + now.getUTCDate() + " " + now.getUTCHours() +
              ":" + now.getUTCMinutes() + ":" + now.getUTCSeconds();
    

    Of course, you'll need to pad the hours, minutes, and seconds to two digits or you'll sometimes get weird looking times like "2011/12/2 19:2:8."

提交回复
热议问题