js日期时间函数(经典+完善+实用)(转采自 :bicabo)

日期时间脚本库方法列表

Date.prototype.isLeapYear 判断闰年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期计算
Date.prototype.DateDiff 比较日期差
Date.prototype.toString 日期转字符串
Date.prototype.toArray 日期分割为数组
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天数
Date.prototype.WeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差

  1. //---------------------------------------------------
  2. // 判断闰年
  3. //---------------------------------------------------
  4. Date.prototype.isLeapYear = function()
  5. {
  6. return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
  7. }
  8. //---------------------------------------------------
  9. // 日期格式化
  10. // 格式 YYYY/yyyy/YY/yy 表示年份
  11. // MM/M 月份
  12. // W/w 星期
  13. // dd/DD/d/D 日期
  14. // hh/HH/h/H 时间
  15. // mm/m 分钟
  16. // ss/SS/s/S 秒
  17. //---------------------------------------------------
  18. Date.prototype.Format = function(formatStr)
  19. {
  20. var str = formatStr;
  21. var Week = [‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘];
  22. str=str.replace(/yyyy|YYYY/,this.getFullYear());
  23. str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():‘0‘ + (this.getYear() % 100));
  24. str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():‘0‘ + this.getMonth());
  25. str=str.replace(/M/g,this.getMonth());
  26. str=str.replace(/w|W/g,Week[this.getDay()]);
  27. str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():‘0‘ + this.getDate());
  28. str=str.replace(/d|D/g,this.getDate());
  29. str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():‘0‘ + this.getHours());
  30. str=str.replace(/h|H/g,this.getHours());
  31. str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():‘0‘ + this.getMinutes());
  32. str=str.replace(/m/g,this.getMinutes());
  33. str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():‘0‘ + this.getSeconds());
  34. str=str.replace(/s|S/g,this.getSeconds());
  35. return str;
  36. }
  37. //+---------------------------------------------------
  38. //| 求两个时间的天数差 日期格式为 YYYY-MM-dd
  39. //+---------------------------------------------------
  40. function daysBetween(DateOne,DateTwo)
  41. {
  42. var OneMonth = DateOne.substring(5,DateOne.lastIndexOf (‘-‘));
  43. var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf (‘-‘)+1);
  44. var OneYear = DateOne.substring(0,DateOne.indexOf (‘-‘));
  45. var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf (‘-‘));
  46. var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf (‘-‘)+1);
  47. var TwoYear = DateTwo.substring(0,DateTwo.indexOf (‘-‘));
  48. var cha=((Date.parse(OneMonth+‘/‘+OneDay+‘/‘+OneYear)- Date.parse(TwoMonth+‘/‘+TwoDay+‘/‘+TwoYear))/86400000);
  49. return Math.abs(cha);
  50. }
  51. //+---------------------------------------------------
  52. //| 日期计算
  53. //+---------------------------------------------------
  54. Date.prototype.DateAdd = function(strInterval, Number) {
  55. var dtTmp = this;
  56. switch (strInterval) {
  57. case ‘s‘ :return new Date(Date.parse(dtTmp) + (1000 * Number));
  58. case ‘n‘ :return new Date(Date.parse(dtTmp) + (60000 * Number));
  59. case ‘h‘ :return new Date(Date.parse(dtTmp) + (3600000 * Number));
  60. case ‘d‘ :return new Date(Date.parse(dtTmp) + (86400000 * Number));
  61. case ‘w‘ :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
  62. case ‘q‘ :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
  63. case ‘m‘ :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
  64. case ‘y‘ :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
  65. }
  66. }
  67. //+---------------------------------------------------
  68. //| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
  69. //+---------------------------------------------------
  70. Date.prototype.DateDiff = function(strInterval, dtEnd) {
  71. var dtStart = this;
  72. if (typeof dtEnd == ‘string‘ )//如果是字符串转换为日期型
  73. {
  74. dtEnd = StringToDate(dtEnd);
  75. }
  76. switch (strInterval) {
  77. case ‘s‘ :return parseInt((dtEnd - dtStart) / 1000);
  78. case ‘n‘ :return parseInt((dtEnd - dtStart) / 60000);
  79. case ‘h‘ :return parseInt((dtEnd - dtStart) / 3600000);
  80. case ‘d‘ :return parseInt((dtEnd - dtStart) / 86400000);
  81. case ‘w‘ :return parseInt((dtEnd - dtStart) / (86400000 * 7));
  82. case ‘m‘ :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
  83. case ‘y‘ :return dtEnd.getFullYear() - dtStart.getFullYear();
  84. }
  85. }
  86. //+---------------------------------------------------
  87. //| 日期输出字符串,重载了系统的toString方法
  88. //+---------------------------------------------------
  89. Date.prototype.toString = function(showWeek)
  90. {
  91. var myDate= this;
  92. var str = myDate.toLocaleDateString();
  93. if (showWeek)
  94. {
  95. var Week = [‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘];
  96. str += ‘ 星期‘ + Week[myDate.getDay()];
  97. }
  98. return str;
  99. }
  100. //+---------------------------------------------------
  101. //| 日期合法性验证
  102. //| 格式为:YYYY-MM-DD或YYYY/MM/DD
  103. //+---------------------------------------------------
  104. function IsValidDate(DateStr)
  105. {
  106. var sDate=DateStr.replace(/(^\s+|\s+$)/g,‘‘); //去两边空格;
  107. if(sDate==‘‘) return true;
  108. //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为‘‘
  109. //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
  110. var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,‘‘);
  111. if (s==‘‘) //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
  112. {
  113. var t=new Date(sDate.replace(/\-/g,‘/‘));
  114. var ar = sDate.split(/[-/:]/);
  115. if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
  116. {
  117. //alert(‘错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。‘);
  118. return false;
  119. }
  120. }
  121. else
  122. {
  123. //alert(‘错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。‘);
  124. return false;
  125. }
  126. return true;
  127. }
  128. //+---------------------------------------------------
  129. //| 日期时间检查
  130. //| 格式为:YYYY-MM-DD HH:MM:SS
  131. //+---------------------------------------------------
  132. function CheckDateTime(str)
  133. {
  134. var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
  135. var r = str.match(reg);
  136. if(r==null)return false;
  137. r[2]=r[2]-1;
  138. var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
  139. if(d.getFullYear()!=r[1])return false;
  140. if(d.getMonth()!=r[2])return false;
  141. if(d.getDate()!=r[3])return false;
  142. if(d.getHours()!=r[4])return false;
  143. if(d.getMinutes()!=r[5])return false;
  144. if(d.getSeconds()!=r[6])return false;
  145. return true;
  146. }
  147. //+---------------------------------------------------
  148. //| 把日期分割成数组
  149. //+---------------------------------------------------
  150. Date.prototype.toArray = function()
  151. {
  152. var myDate = this;
  153. var myArray = Array();
  154. myArray[0] = myDate.getFullYear();
  155. myArray[1] = myDate.getMonth();
  156. myArray[2] = myDate.getDate();
  157. myArray[3] = myDate.getHours();
  158. myArray[4] = myDate.getMinutes();
  159. myArray[5] = myDate.getSeconds();
  160. return myArray;
  161. }
  162. //+---------------------------------------------------
  163. //| 取得日期数据信息
  164. //| 参数 interval 表示数据类型
  165. //| y 年 m月 d日 w星期 ww周 h时 n分 s秒
  166. //+---------------------------------------------------
  167. Date.prototype.DatePart = function(interval)
  168. {
  169. var myDate = this;
  170. var partStr=‘‘;
  171. var Week = [‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘];
  172. switch (interval)
  173. {
  174. case ‘y‘ :partStr = myDate.getFullYear();break;
  175. case ‘m‘ :partStr = myDate.getMonth()+1;break;
  176. case ‘d‘ :partStr = myDate.getDate();break;
  177. case ‘w‘ :partStr = Week[myDate.getDay()];break;
  178. case ‘ww‘ :partStr = myDate.WeekNumOfYear();break;
  179. case ‘h‘ :partStr = myDate.getHours();break;
  180. case ‘n‘ :partStr = myDate.getMinutes();break;
  181. case ‘s‘ :partStr = myDate.getSeconds();break;
  182. }
  183. return partStr;
  184. }
  185. //+---------------------------------------------------
  186. //| 取得当前日期所在月的最大天数
  187. //+---------------------------------------------------
  188. Date.prototype.MaxDayOfDate = function()
  189. {
  190. var myDate = this;
  191. var ary = myDate.toArray();
  192. var date1 = (new Date(ary[0],ary[1]+1,1));
  193. var date2 = date1.dateAdd(1,‘m‘,1);
  194. var result = dateDiff(date1.Format(‘yyyy-MM-dd‘),date2.Format(‘yyyy-MM-dd‘));
  195. return result;
  196. }
  197. //+---------------------------------------------------
  198. //| 取得当前日期所在周是一年中的第几周
  199. //+---------------------------------------------------
  200. Date.prototype.WeekNumOfYear = function()
  201. {
  202. var myDate = this;
  203. var ary = myDate.toArray();
  204. var year = ary[0];
  205. var month = ary[1]+1;
  206. var day = ary[2];
  207. document.write(‘< script language=VBScript\> \n‘);
  208. document.write(‘myDate = DateValue(‘‘+month+‘-‘+day+‘-‘+year+‘‘) \n‘);
  209. document.write(‘result = DatePart(‘ww‘, myDate) \n‘);
  210. document.write(‘ \n‘);
  211. return result;
  212. }
  213. //+---------------------------------------------------
  214. //| 字符串转成日期类型
  215. //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
  216. //+---------------------------------------------------
  217. function StringToDate(DateStr)
  218. {
  219. var converted = Date.parse(DateStr);
  220. var myDate = new Date(converted);
  221. if (isNaN(myDate))
  222. {
  223. //var delimCahar = DateStr.indexOf(‘/‘)!=-1?‘/‘:‘-‘;
  224. var arys= DateStr.split(‘-‘);
  225. myDate = new Date(arys[0],--arys[1],arys[2]);
  226. }
  227. return myDate;
  228. }
  229. // 输入一个日期输出后一天的日期 日期格式为 YYYY-MM-dd
    function nextdate(date)
    { var date = new Date(date); //传入一个时间格式,如果不传入就是获取现在的时间了
    var time = date.getTime();//毫秒数的时间戳
    var next_time=time+(3600*24*1000);//时间戳为毫秒数,要*1000
    var date = new Date(next_time);//Tue Sep 01 2015 08:01:26 GMT+0800 (中国标准时间)
    var Y = date.getFullYear() + ‘-‘;
    var M = (date.getMonth()+1 < 10 ? ‘0‘+(date.getMonth()+1) : date.getMonth()+1) + ‘-‘;
    var D = (date.getDate()< 10 ? ‘0‘+date.getDate() : date.getDate());
    return (Y+M+D);

    }

    // 输入一个日期输出前一天的日期 日期格式为 YYYY-MM-dd
    function predate(date)
    { var date = new Date(date); //传入一个时间格式,如果不传入就是获取现在的时间了
    var time = date.getTime();//毫秒数的时间戳
    var next_time=time+(3600*24*1000);//时间戳为毫秒数,要*1000
    var date = new Date(next_time);//Tue Sep 01 2015 08:01:26 GMT+0800 (中国标准时间)
    var Y = date.getFullYear() + ‘-‘;
    var M = (date.getMonth()+1 < 10 ? ‘0‘+(date.getMonth()+1) : date.getMonth()+1) + ‘-‘;
    var D = (date.getDate()< 10 ? ‘0‘+date.getDate() : date.getDate());
    return (Y+M+D);

    }

Date.prototype.Format 日期格式化方法有点问题,月份错了~

时间: 2024-08-01 22:44:22

js日期时间函数(经典+完善+实用)(转采自 :bicabo)的相关文章

mysql学习笔记(五)--- 字符串函数、日期时间函数

一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左.取右 6.LENGTH   获取长度(字节为单位) 7.LTRIM/RTRIM/TRIM 去空格(左/右/自定义) 8.STRCMP  字符串比较 9.CONCAT  字符串拼接 10.SUBSTRING  字符串截取 1.CHAR_LENGTH:获取长度(字符为单位) CHAR_LENGTH()

PHP 中日期时间函数 date() 用法总结

[导读] date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考.格式化日期date() 函数的第一个参数规定了如何格式化日期 时间.它使用字母来表示日期和时间 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母来表示日期和时间的格式.这里列出了一些可用的字母: •d - 月中的天 (01-31)•m - 当前月,以数字计 (01-12)•Y - 当前的年(四位数)您可以在我们的 PHP Date 参考手

MySQL字符串函数、日期时间函数

MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左.取右 6.LENGTH   获取长度(字节为单位) 7.LTRIM/RTRIM/TRIM 去空格(左/右/自定义) 8.STRCMP  字符串比较 9.CONCAT  字符串拼接 10.SUBSTRING  字符串截取 1.CHAR_LENGTH:获取长度(字符

DELPHI日期时间函数(DateUtils单元)

原文出自:http://shao171.blog.163.com/blog/static/238397019201502011844293/ CompareDate 函数 比较两个日期时间值日期部分的大小 CompareDateTime 函数 比较两个日期时间值的大小 CompareTime 函数 比较两个日期时间值时间部分的大小 DateOf 函数 去除日期时间值的时间部分 DateTimeToJulianDate 函数 转换日期时间值为儒略日 DateTimeToModifiedJulian

日期时间函数(需要用变量调用):

日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b.getMonth()+1; //获取月份b.getDate() //获取天b.getHours() //获取小时b.getMinutes() //获取分钟b.getSeconds() //获取秒数b.getDay() //获取星期几b.getMilliseconds() //获取毫秒 数学函数(用Math来调用):abs(x

php学习笔记数组与数据结构1(日期时间函数及遇到的问题解决)

1在PHP中从最简单的数组构建: 1)常用函数: 生成随机数: 1 echo rand(1,10);//两个参数来确定随机数的范围 2 echo rand(10);10//被当成了他的种子,即不能只给一个参数 2的结果是(缺少一个参数) 2)日期时间函数 <?php var_dump(time());//取当前时间的UNIX时间戳 ?> 这样显示的结果: 例:类似于这样(前提是用var_dump()输出返回的值) 正确的方式是 *年份year(四位)用大写的Y,月份month用小写m,天day

1009,数据库查询,聚合函数,日期时间函数

SELECT 语句的逻辑处理顺序     between——在多少数和多少数之间 from on join where      ——筛选条件 ( 比较运算符,< > <= >= != ) ( 逻辑运算符, ang   or ) group by   ——对语句进行分组 with cube 或 with  rollup having select distinct   ——去除重复的 order by   ——默认从低到高排序asc(升序)  尾部写desc 从高到底排序(降序)

ORACLE日期时间函数大全

ORACLE日期时间函数大全    TO_DATE格式(以时间:2007-11-02   13:45:25为例) Year:              yy two digits 两位年                显示值:07        yyy three digits 三位年                显示值:007        yyyy four digits 四位年                显示值:2007 Month:              mm    numbe

php date()日期时间函数详解

PHP(PHP培训 php教程 )中date()日期时间函数详解,需要的朋友可以参考下. 1,年-月-日 echo date('Y-m-j'); 2007-02-6 echo date('y-n-j'); 07-2-6 大写Y表示年四位数字,而小写y表示年的两位数字; 小写m表示月份的数字(带前导),而小写n则表示不带前导的月份数字. echo date('Y-M-j'); 2007-Feb-6 echo date('Y-m-d'); 2007-02-06 大写M表示月份的3个缩写字符,而小写m