Javascript:以 YYYY/mm/dd hh:m:sec 格式输出当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8362952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: output current datetime in YYYY/mm/dd hh:m:sec format
提问by sarsnake
I need to output the current UTC datetime as a string with the following format:YYYY/mm/dd hh:m:sec
我需要将当前 UTC 日期时间输出为具有以下格式的字符串:YYYY/mm/dd hh:m:sec
How do I achieve that with Javascript?
我如何使用 Javascript 实现这一目标?
回答by Joseph Marikle
You can build it manually:
您可以手动构建它:
var m = new Date();
var dateString = m.getUTCFullYear() +"/"+ (m.getUTCMonth()+1) +"/"+ m.getUTCDate() + " " + m.getUTCHours() + ":" + m.getUTCMinutes() + ":" + m.getUTCSeconds();
and to force two digits on the values that require it, you can use something like this:
并在需要它的值上强制使用两位数,您可以使用以下内容:
("0000" + 5).slice(-2)
Which would look like this:
看起来像这样:
var m = new Date();
var dateString =
m.getUTCFullYear() + "/" +
("0" + (m.getUTCMonth()+1)).slice(-2) + "/" +
("0" + m.getUTCDate()).slice(-2) + " " +
("0" + m.getUTCHours()).slice(-2) + ":" +
("0" + m.getUTCMinutes()).slice(-2) + ":" +
("0" + m.getUTCSeconds()).slice(-2);
console.log(dateString);
回答by MarredCheese
No library, one line, properly padded
没有图书馆,一行,正确填充
const str = (new Date()).toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ");
It uses the built-in function Date.toISOString()
, chops off the ms, replaces the hyphens with slashes, and replaces the T with a space to go from say '2019-01-05T09:01:07.123'
to '2019/01/05 09:01:07'
.
它使用内置函数Date.toISOString()
,砍掉 ms,用斜杠替换连字符,并用空格替换 T 以从 say'2019-01-05T09:01:07.123'
到'2019/01/05 09:01:07'
.
Local time instead of UTC
本地时间而不是 UTC
const now = new Date();
const offsetMs = now.getTimezoneOffset() * 60 * 1000;
const dateLocal = new Date(now.getTime() - offsetMs);
const str = dateLocal.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ");
回答by user3452845
With jQuery date format :
使用 jQuery 日期格式:
$.format.date(new Date(), 'yyyy/MM/dd HH:mm:ss');
https://github.com/phstc/jquery-dateFormat
https://github.com/phstc/jquery-dateFormat
Enjoy
享受
回答by sean1093
I wrote a simple library for manipulating the JavaScript date object. You can try this:
我编写了一个简单的库来操作 JavaScript 日期对象。你可以试试这个:
var dateString = timeSolver.getString(new Date(), "YYYY/MM/DD HH:MM:SS.SSS")
Library here: https://github.com/sean1093/timeSolver
回答by dgvid
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."
当然,您需要将小时、分钟和秒填充为两位数,否则有时会出现看起来很奇怪的时间,例如“2011/12/2 19:2:8”。
回答by Nikos M.
Posting another script solution DateX(author) for anyone interested
为任何感兴趣的人发布另一个脚本解决方案DateX(作者)
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.
DateX 不包装原始Date
对象,而是提供了一个相同的接口,带有其他方法来轻松地格式化、本地化、解析、差异和验证日期。因此,人们可以new DateX(..)
代替new Date(..)
或使用 lib 作为日期实用程序,甚至作为Date
类的包装器或替代品。
The date format used is identical to php date format.
使用的日期格式与php 日期格式相同。
c-like formatis also supported (although not fully)
也支持类似 c 的格式(虽然不完全)
for the example posted (YYYY/mm/dd hh:m:sec
) the format to use would be Y/m/d H:i:s
eg
对于发布的示例 ( YYYY/mm/dd hh:m:sec
),要使用的格式将是Y/m/d H:i:s
例如
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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC