繁体   English   中英

JS。 转换格式日期

[英]JS. Convert format date

我有这种格式的日期:

var date

2013年5月31日星期五17:41:01 GMT + 0200(CEST)

如何转换为: 31.05.2013

编辑:如果您必须经常处理日期和时间,此答案很好。 也许这就是您要寻找的。 它使我的工作更加轻松。 值得一看。 如果仅涉及此简单任务,请不要加载新脚本。

我推荐moment.js: http ://momentjs.com/

简单的编码示例:

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);

试试看: http : //jsfiddle.net/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)");

没有格式化程序,请执行以下操作:

('0' + date.getDate()).slice(-2) + '.' +
('0' + (date.getMonth() + 1)).slice(-2) + '.' + 
date.getFullYear()

如果dDate对象(而不是表示日期的字符串),则可以使用此方法

var d = new Date();

("0" + d.getDate()).slice(-2) + "." + 
("0" + (d.getMonth() + 1)).slice(-2) + "." + 
d.getFullYear();

否则,如果您有字符串,则首先将其作为参数传递给Date构造函数

var d = new Date("Fry May 31...");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM