var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
日期时间脚本库方法列表
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 日期天数差
004 |
Date.prototype.isLeapYear = function()
|
006 |
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
|
019 |
Date.prototype.Format = function(formatStr)
|
022 |
var Week = ['日','一','二','三','四','五','六'];
|
024 |
str=str.replace(/yyyy|YYYY/,this.getFullYear());
|
025 |
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
|
027 |
str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
|
028 |
str=str.replace(/M/g,this.getMonth());
|
030 |
str=str.replace(/w|W/g,Week[this.getDay()]);
|
032 |
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
|
033 |
str=str.replace(/d|D/g,this.getDate());
|
035 |
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
|
036 |
str=str.replace(/h|H/g,this.getHours());
|
037 |
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
|
038 |
str=str.replace(/m/g,this.getMinutes());
|
040 |
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
|
041 |
str=str.replace(/s|S/g,this.getSeconds());
|
049 |
function daysBetween(DateOne,DateTwo)
|
051 |
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
|
052 |
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
|
053 |
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
|
055 |
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
|
056 |
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
|
057 |
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
|
059 |
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
|
060 |
return Math.abs(cha);
|
067 |
Date.prototype.DateAdd = function(strInterval, Number) {
|
069 |
switch (strInterval) {
|
070 |
case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
|
071 |
case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
|
072 |
case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
|
073 |
case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
|
074 |
case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
|
075 |
case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
|
076 |
case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
|
077 |
case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
|
084 |
Date.prototype.DateDiff = function(strInterval, dtEnd) {
|
086 |
if (typeof dtEnd == 'string' )
|
088 |
dtEnd = StringToDate(dtEnd);
|
090 |
switch (strInterval) {
|
091 |
case 's' :return parseInt((dtEnd - dtStart) / 1000);
|
092 |
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
|
093 |
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
|
094 |
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
|
095 |
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
|
096 |
case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
|
097 |
case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
|
104 |
Date.prototype.toString = function(showWeek)
|
107 |
var str = myDate.toLocaleDateString();
|
110 |
var Week = ['日','一','二','三','四','五','六'];
|
111 |
str += ' 星期' + Week[myDate.getDay()];
|
120 |
function IsValidDate(DateStr)
|
122 |
var sDate=DateStr.replace(/(^\s+|\s+$)/g,'');
|
123 |
if(sDate=='') return true;
|
126 |
var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
|
129 |
var t=new Date(sDate.replace(/\-/g,'/'));
|
130 |
var ar = sDate.split(/[-/:]/);
|
131 |
if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
|
149 |
function CheckDateTime(str)
|
151 |
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
|
152 |
var r = str.match(reg);
|
153 |
if(r==null)return false;
|
155 |
var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
|
156 |
if(d.getFullYear()!=r[1])return false;
|
157 |
if(d.getMonth()!=r[2])return false;
|
158 |
if(d.getDate()!=r[3])return false;
|
159 |
if(d.getHours()!=r[4])return false;
|
160 |
if(d.getMinutes()!=r[5])return false;
|
161 |
if(d.getSeconds()!=r[6])return false;
|
168 |
Date.prototype.toArray = function()
|
171 |
var myArray = Array();
|
172 |
myArray[0] = myDate.getFullYear();
|
173 |
myArray[1] = myDate.getMonth();
|
174 |
myArray[2] = myDate.getDate();
|
175 |
myArray[3] = myDate.getHours();
|
176 |
myArray[4] = myDate.getMinutes();
|
177 |
myArray[5] = myDate.getSeconds();
|
186 |
Date.prototype.DatePart = function(interval)
|
190 |
var Week = ['日','一','二','三','四','五','六'];
|
193 |
case 'y' :partStr = myDate.getFullYear();break;
|
194 |
case 'm' :partStr = myDate.getMonth()+1;break;
|
195 |
case 'd' :partStr = myDate.getDate();break;
|
196 |
case 'w' :partStr = Week[myDate.getDay()];break;
|
197 |
case 'ww' :partStr = myDate.WeekNumOfYear();break;
|
198 |
case 'h' :partStr = myDate.getHours();break;
|
199 |
case 'n' :partStr = myDate.getMinutes();break;
|
200 |
case 's' :partStr = myDate.getSeconds();break;
|
208 |
Date.prototype.MaxDayOfDate = function()
|
211 |
var ary = myDate.toArray();
|
212 |
var date1 = (new Date(ary[0],ary[1]+1,1));
|
213 |
var date2 = date1.dateAdd(1,'m',1);
|
214 |
var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
|
221 |
Date.prototype.WeekNumOfYear = function()
|
224 |
var ary = myDate.toArray();
|
226 |
var month = ary[1]+1;
|
228 |
document.write('< script language=VBScript\> \n');
|
229 |
document.write('myDate = DateValue('+month+'-'+day+'-'+year+') \n');
|
230 |
document.write("result = DatePart('ww', myDate) \n");
|
231 |
document.write(' \n');
|
239 |
function StringToDate(DateStr)
|
242 |
var converted = Date.parse(DateStr);
|
243 |
var myDate = new Date(converted);
|
247 |
var arys= DateStr.split('-');
|
248 |
myDate = new Date(arys[0],--arys[1],arys[2]);
|
附加下sql日期格式
Sql Server 常用日期格式
SQL Server中文版的默认的日期字段datetime格式是yyyy-mm-dd Thh:mm:ss.mmm
例如:
select getdate()
2004-09-12 11:06:08.177
整理了一下SQL Server里面可能经常会用到的日期格式转换方法:
举例如下:
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177