JS如何获取日期

老牛10岁了 2011-08-02 03:20:37
怎么获取格式为yyyy-MM-dd的当前日期呢?


我用new Date()获取出来的日期不知道是什么格式的。想转成2011-08-02。。怎么弄?
...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
老徐bozin 2011-08-03
  • 打赏
  • 举报
回复
还是取服务器时间把
BLUE_LG 2011-08-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/ayqxm/article/details/6424674
kyzy_yy_pm 2011-08-02
  • 打赏
  • 举报
回复

DD = new Date();
y = DD.getFullYear();
m = DD.getMonth() < 9 ? "0" + (DD.getMonth() + 1) : DD.getMonth() + 1;
d = DD.getDate() < 10 ? "0" + DD.getDate() : DD.getDate();
s = y + "年" + m + "月" + d + "日";
alert(s);
q107770540 2011-08-02
  • 打赏
  • 举报
回复


<script language="JavaScript">
Date.prototype.format = function(format) //author: meizz
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
alert(new Date().format("yyyy-MM-dd"));
alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss"));
</script>

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧