Javascript 在 jQuery 中格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9050763/
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
Format date in jQuery
提问by Mark Fondy
var date = "Fri Jan 29 2012 06:12:00 GMT+0100";
How can i show this in format 2012-01-29 06:12? In PHP is function ->format. In Javascript is also format, but if i try use this then i have error:
我如何以2012-01-29 06:12格式显示?在 PHP 中是函数 -> 格式。在 Javascript 中也是格式,但是如果我尝试使用它,则会出现错误:
now.format is not a function
now.format 不是函数
var now = new Date();
console.log(now.format("isoDateTime"));
I would like receive format: 2012-01-29 06:12
我要接收格式: 2012-01-29 06:12
回答by Tadeck
This question is a duplicate (see: How to get current date in jquery?).
这个问题是重复的(请参阅:如何在 jquery 中获取当前日期?)。
By modifying my solutionfrom the other question, I got:
通过从另一个问题修改我的解决方案,我得到了:
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
var output = d.getFullYear() + '-' +
((''+month).length<2 ? '0' : '') + month + '-' +
((''+day).length<2 ? '0' : '') + day + ' ' +
((''+hour).length<2 ? '0' :'') + hour + ':' +
((''+minute).length<2 ? '0' :'') + minute + ':' +
((''+second).length<2 ? '0' :'') + second;
See this jsfiddle for a proof: http://jsfiddle.net/nCE9u/3/
请参阅此 jsfiddle 以获取证明:http: //jsfiddle.net/nCE9u/3/
You can also enclose it within function (demo is here: http://jsfiddle.net/nCE9u/4/):
您也可以将其包含在函数中(演示在这里:http: //jsfiddle.net/nCE9u/4/):
function getISODateTime(d){
// padding function
var s = function(a,b){return(1e15+a+"").slice(-b)};
// default date parameter
if (typeof d === 'undefined'){
d = new Date();
};
// return ISO datetime
return d.getFullYear() + '-' +
s(d.getMonth()+1,2) + '-' +
s(d.getDate(),2) + ' ' +
s(d.getHours(),2) + ':' +
s(d.getMinutes(),2) + ':' +
s(d.getSeconds(),2);
}
and use it like that:
并像这样使用它:
getISODateTime(new Date());
or:
或者:
getISODateTime(some_other_date);
EDIT:I have added some improvement to the function, as proposed by Ates Goral (also decreased its readability in favour of code comments).
编辑:我对函数进行了一些改进,正如 Ates Goral 提出的那样(也降低了它的可读性以支持代码注释)。
回答by Mark Brackett
回答by GreyBeardedGeek
Unfortunately, in Javascript, Date does not have a format() method.
不幸的是,在 Javascript 中,Date 没有 format() 方法。
Check out http://fisforformat.sourceforge.netfor some nice formatting methods.
查看http://fisforformat.sourceforge.net了解一些不错的格式化方法。
回答by Ates Goral
Use a library like Datejsor perhaps this tweet-sizedimplementation:
https://gist.github.com/1005948
https://gist.github.com/1005948
var str = formatDate(
new Date(),
"{FullYear}-{Month:2}-{Date:2} {Hours:2}:{Minutes:2}");
回答by helloyou
I think this could be help you:date.format.js
我认为这可以帮助你:date.format.js
var now = new Date();
now.format("m/dd/yy");
// Returns, e.g., 6/09/07
// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM
// You can use one of several named masks
now.format("isoDateTime");