最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS. Convert format date - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 3

EDIT: 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...");
发布评论

评论列表(0)

  1. 暂无评论