javascript代码:不用if来获取8位日期,有更好的办法么?

woshihuzi 2008-09-01 09:56:06
var dbldigits = "0001020304050607080910111213141516171819202122232425262728293031";
var myDate = new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
var syyyymmdd = year + dbldigits.substring(month*2,month*2+2) + dbldigits.substring(day*2,day*2+2);

我记得VBscript里面有,但js我想不出更好的办法。
如果有,请不吝赐教。谢谢!
...全文
286 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbzy123 2008-09-02
  • 打赏
  • 举报
回复

<script language="javascript" type="text/javascript">
var date = new Date();
alert(date.toLocaleDateString());
</script>
woshihuzi 2008-09-02
  • 打赏
  • 举报
回复
谢谢各位帮忙,代码都不错!其中lqscoke提供的函数最好用!
lqscoke 2008-09-01
  • 打赏
  • 举报
回复
<script language="javascript"> 
//补0函数
function appendZero(s){
return ("00"+ s).substr((s+"").length);
}
var d = new Date();
alert(d.getFullYear() +"-" +appendZero(d.getMonth() +1) + "-" + appendZero(d.getDate()));
</script>
lqscoke 2008-09-01
  • 打赏
  • 举报
回复
也不能用正则吗?
woshihuzi 2008-09-01
  • 打赏
  • 举报
回复
我感觉这样是自己找麻烦,如果要形成hhmmss格式的6位时间字符串,还不要定义一个120位的字符串?
所以在此求教一个更简单的方法,但不喜欢用if。
hbhbhbhbhb1021 2008-09-01
  • 打赏
  • 举报
回复

<script>
function Date.prototype.toString(){
var tempMonth=this.getMonth()+1;
tempMonth=(tempMonth.toString().length==1)?("0"+tempMonth):tempMonth;
var tempDate=this.getDate();
tempDate=(tempDate.toString().length==1)?("0"+tempDate):tempDate;
return this.getFullYear()+"年"+tempMonth+"月"+tempDate+"日";
}
alert(new Date())
</script>

87,910

社区成员

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

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