如何将 jQuery 中的日期从完整格式格式化为 YYYYMMDD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20900767/
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
How to format a date in jQuery from full to YYYYMMDD?
提问by Francesca
I'm using AJAX to grab data from an XML file and print it back out to me. One of those is a date, and uses this code:
我正在使用 AJAX 从 XML 文件中获取数据并将其打印回给我。其中之一是日期,并使用以下代码:
jQuery(function(){
$.ajax({
url: 'http://www.sagittarius-digital.com/news.rss',
dataType: 'xml'
}).done(function(xml){
var items = $(xml).find('item').map(function(){
var $item = $(this);
var array = '<div class="item">';
array += '<p>' + $item.find('pubDate').text() + '</p>';
return array;
So the line:
所以这一行:
array += '<p>' + $item.find('pubDate').text() + '</p>';
brings back a date like this:
带回这样的日期:
Mon, 18 Nov 2013 12:00:00 GMT
How can I cast this into YYYMMDD? Time is not required.
如何将其转换为 YYYMMDD?不需要时间。
Live version in action(big buggy, take a while to load)
实时版本(大马车,加载需要一段时间)
回答by adeneo
Looks like a valid date to me, so parse it with new Date()
对我来说看起来像一个有效的日期,所以解析它 new Date()
var date = new Date('Mon, 18 Nov 2013 12:00:00 GMT');
In your code, something like
在您的代码中,类似
ajax.done(function(xml){
var items = $(xml).find('item').map(function(){
var $item = $(this);
var array = '<div class="item">';
var date = new Date( $item.find('pubDate').text() );
var year = pad(date.getFullYear());
var month = pad(date.getMonth() + 1);
var day = pad(date.getDate());
var yyyymmdd = year + month + day;
array += '<p>' + yyyymmdd + '</p>';
return array;
});
function pad(numb) {
return (numb < 10 ? '0' : '') + numb;
}
回答by Ogglas
var date = new Date($.now());
var dateString = (date.getFullYear() + '-'
+ ('0' + (date.getMonth() + 1)).slice(-2)
+ '-' + ('0' + (date.getDate())).slice(-2));
console.log(dateString); //Will print "2015-09-18" when I wrote this comment
To explain, .slice(-2) gives us the last two characters of the string.
解释一下,.slice(-2) 给了我们字符串的最后两个字符。
So no matter what, we can add "0" to the day or month, and just ask for the last two since those are always the two we want.
所以无论如何,我们可以在日或月后添加“0”,并只要求最后两个,因为那些总是我们想要的两个。
So if the MyDate.getMonth() returns 9, it will be:
所以如果 MyDate.getMonth() 返回 9,它将是:
("0" + "9") // Giving us "09"
so adding .slice(-2) on that gives us the last two characters which is:
所以添加 .slice(-2) 给我们最后两个字符,即:
("0" + "9").slice(-2)
"09"
But if date.getMonth() returns 10, it will be:
但是如果 date.getMonth() 返回 10,它将是:
("0" + "10") // Giving us "010"
so adding .slice(-2) gives us the last two characters, or:
所以添加 .slice(-2) 给我们最后两个字符,或者:
("0" + "10").slice(-2)
"10"
回答by Nishu Tayal
You can process this with Dateobject and get the format as per your need.
您可以使用Date对象处理它并根据需要获取格式。
var yourdate = $item.find('pubDate').text();
// Above gives you date in String form. But for getting date,
first create in date object like below
var dt = new Date(yourdate);
var newDt = dt.getFullYear()+""+(dt.getMonth()+1)+""+dt.getDate();
// Output : 20131118
array += '<p>' + newDt + '</p>';
Here since getMonth() gives (0-11) month, so add 1.
这里因为 getMonth() 给出了 (0-11) 个月,所以加 1。
For more details you can check : http://www.w3schools.com/jsref/jsref_obj_date.asp
有关更多详细信息,您可以查看:http: //www.w3schools.com/jsref/jsref_obj_date.asp