- js日期比较
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta name="Generator" content="EditPlus">
- <meta name="Author" content="">
- <meta name="Keywords" content="">
- <meta name="Description" content="">
- </head>
- <body>
- <script type="text/javascript">
- <!--
- //js日期比较(yyyy-mm-dd)
- function duibi(a, b) {
- var arr = a.split("-");
- var starttime = new Date(arr[0], arr[1], arr[2]);
- var starttimes = starttime.getTime();
- var arrs = b.split("-");
- var lktime = new Date(arrs[0], arrs[1], arrs[2]);
- var lktimes = lktime.getTime();
- if (starttimes >= lktimes) {
- alert('开始时间大于离开时间,请检查');
- return false;
- } else {
- return true;
- }
- }
- //js时间比较(yyyy-mm-dd hh:mi:ss)
- function comptime() {
- var beginTime = "2009-09-21 00:00:00";
- var endTime = "2009-09-21 00:00:01";
- var beginTimes = beginTime.substring(0, 10).split('-');
- var endTimes = endTime.substring(0, 10).split('-');
- beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
- endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);
- alert(beginTime + "aaa" + endTime);
- alert(Date.parse(endTime));
- alert(Date.parse(beginTime));
- var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
- if (a < 0) {
- alert("endTime小!");
- } else if (a > 0) {
- alert("endTime大!");
- } else if (a == 0) {
- alert("时间相等!");
- } else {
- return 'exception'
- }
- }
- duibi('2014-15-12','2013-12-05');
- //-->
- </script>
- </body>
- </html>
- ------------------------------------------
- js日期格式化
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title> New Document </title>
- <meta name="Generator" content="EditPlus">
- <meta name="Author" content="">
- <meta name="Keywords" content="">
- <meta name="Description" content="">
- </head>
- <body>
- <script type="text/javascript">
- <!--
- //用JavaScript(js)对时间格式化
- Date.prototype.format =function(format)
- {
- 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;
- }
- var d =new Date().format('yyyy-MM-dd');
- alert(d);
- //-->
- </script>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/120520149533.html
来源: http://www.codesnippet.cn/detail/120520149533.html