Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449338
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: JavaScript

2014-03-30 07:06:36

js的函数代码。

  1. Date.prototype.format = function(format){
  2. var o = {
  3. "M+" : this.getMonth()+1, //month
  4. "d+" : this.getDate(), //day
  5. "h+" : this.getHours(), //hour
  6. "m+" : this.getMinutes(), //minute
  7. "s+" : this.getSeconds(), //second
  8. "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  9. "S" : this.getMilliseconds() //millisecond
  10. }


  11. if(/(y+)/.test(format)) {
  12. format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  13. }


  14. for(var k in o) {
  15. if(new RegExp("("+ k +")").test(format)) {
  16. format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
  17. }
  18. }
  19. return format;
  20. }

  21. //使用方法
  22. var now = new Date();
  23. var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
  24. //使用方法2:
  25. var testDate = new Date();
  26. var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
  27. alert(testStr);
  28. //示例: (脚本学堂 )
  29. alert(new Date().Format("yyyy年MM月dd日"));
  30. alert(new Date().Format("MM/dd/yyyy"));
  31. alert(new Date().Format("yyyyMMdd"));
  32. alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
例2,js格式化当前时间为yyyy-mm-dd形式 
  1. function getNowFormatDate()
  2. {
  3. var day = new Date();
  4. var Year = 0;
  5. var Month = 0;
  6. var Day = 0;
  7. var CurrentDate = "";
  8. //初始化时间
  9. //Year= day.getYear();//有火狐下2008年显示108的bug
  10. Year= day.getFullYear();//ie火狐下都可以
  11. Month= day.getMonth()+1;
  12. Day = day.getDate();
  13. //Hour = day.getHours();
  14. // Minute = day.getMinutes();
  15. // Second = day.getSeconds();
  16. CurrentDate += Year + "-";
  17. if (Month >= 10 )
  18. {
  19. CurrentDate += Month + "-";
  20. } (脚本学堂 www.jbxue.com)
  21. else
  22. {
  23. CurrentDate += "0" + Month + "-";
  24. }
  25. if (Day >= 10 )
  26. {
  27. CurrentDate += Day ;
  28. }
  29. else
  30. {
  31. CurrentDate += "0" + Day ;
  32. }
  33. return CurrentDate;
  34. }
阅读(2493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~