admin管理员组文章数量:1323225
I have date in this format:
var date
:
Fri May 31 2013 17:41:01 GMT+0200 (CEST)
How to convert this to: 31.05.2013
?
I have date in this format:
var date
:
Fri May 31 2013 17:41:01 GMT+0200 (CEST)
How to convert this to: 31.05.2013
?
Share Improve this question edited Jun 25, 2013 at 16:00 herman 12.3k5 gold badges49 silver badges63 bronze badges asked Jun 25, 2013 at 15:43 happydevhappydev 1611 silver badge8 bronze badges 1- 2 possible duplicate, see stackoverflow./questions/1056728/… – herman Commented Jun 25, 2013 at 15:45
4 Answers
Reset to default 3EDIT: This answer is good if you've to handle dates and times often. Maybe it's what you're looking for. It made my work much easier. It's worth a look. If it's just about this simple task, don't load a new script.
I rement moment.js: http://momentjs./
Simple coded example:
var date = new Date("Fri May 31 2013 17:41:01 GMT+0200 (CEST)");
var date_str = moment(date).format("DD.MM.YYYY");
alert(date_str);
Try it: http://jsfiddle/bvaLt/
function convertDate(str){
var d = new Date(str);
return addZero(d.getDate())+"."+addZero(d.getMonth()+1)+"."+d.getFullYear();
}
//If we have the number 9, display 09 instead
function addZero(num){
return (num<10?"0":"")+num;
}
convertDate("Fri May 31 2013 17:41:01 GMT+0200 (CEST)");
Without a formatter, go for:
('0' + date.getDate()).slice(-2) + '.' +
('0' + (date.getMonth() + 1)).slice(-2) + '.' +
date.getFullYear()
If d
is a Date
Object (and not a string representing the date) you may use this approach
var d = new Date();
("0" + d.getDate()).slice(-2) + "." +
("0" + (d.getMonth() + 1)).slice(-2) + "." +
d.getFullYear();
otherwise, if you have a string, as a first step just pass it into the Date
constructor as argument
var d = new Date("Fry May 31...");
本文标签: javascriptJS Convert format dateStack Overflow
版权声明:本文标题:javascript - JS. Convert format date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742078710a2419564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论