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 日期天数差
js代码:
//---------------------------------------------------
// 判断闰年
//---------------------------------------------------
Date.prototype.isLeapYear = function()
{
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)))
}
//---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM/M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
//---------------------------------------------------
Date.prototype.Format = function(formatStr)
{
var str = formatStr
var Week = ['日','一','二','三','四','五','六']
str=str.replace(/yyyy|YYYY/,this.getFullYear())
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100))
str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth())
str=str.replace(/M/g,this.getMonth())
str=str.replace(/w|W/g,Week[this.getDay()])
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate())
str=str.replace(/d|D/g,this.getDate())
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours())
str=str.replace(/h|H/g,this.getHours())
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes())
str=str.replace(/m/g,this.getMinutes())
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds())
str=str.replace(/s|S/g,this.getSeconds())
return str
}
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
//+---------------------------------------------------
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'))
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1)
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'))
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'))
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1)
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'))
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000)
return Math.abs(cha)
}
//+---------------------------------------------------
//| 日期计算
//+---------------------------------------------------
Date.prototype.DateAdd = function(strInterval, Number) {
var dtTmp = this
switch (strInterval) {
case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number))
case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number))
case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number))
case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number))
case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number))
case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) +
Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
dtTmp.getSeconds())
case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) +
Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
dtTmp.getSeconds())
case 'y' :return new Date((dtTmp.getFullYear() + Number),
dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
dtTmp.getSeconds())
}
}
//+---------------------------------------------------
//| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串
//+---------------------------------------------------
Date.prototype.DateDiff = function(strInterval, dtEnd) {
var dtStart = this
if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
{
dtEnd = StringToDate(dtEnd)
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000)
case 'n' :return parseInt((dtEnd - dtStart) / 60000)
case 'h' :return parseInt((dtEnd - dtStart) / 3600000)
case 'd' :return parseInt((dtEnd - dtStart) / 86400000)
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7))
case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1)
case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear()
}
}
//+---------------------------------------------------
//| 日期输出字符串,重载了系统的toString方法
//+---------------------------------------------------
Date.prototype.toString = function(showWeek)
{
var myDate= this
var str = myDate.toLocaleDateString()
if (showWeek)
{
var Week = ['日','一','二','三','四','五','六']
str += ' 星期' + Week[myDate.getDay()]
}
return str
}
//+---------------------------------------------------
//| 日期合法性验证
//| 格式为:YYYY-MM-DD或YYYY/MM/DD
//+---------------------------------------------------
function IsValidDate(DateStr)
{
var sDate=DateStr.replace(/(^\s+|\s+$)/g,'')//去两边空格
if(sDate=='') return true
//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
//数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'')
if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
var t=new Date(sDate.replace(/\-/g,'/'))
var ar = sDate.split(/[-/:]/)
if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。')
return false
}
}
else
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。')
return false
}
return true
}
//+---------------------------------------------------
//| 日期时间检查
//| 格式为:YYYY-MM-DD HH:MM:SS
//+---------------------------------------------------
function CheckDateTime(str)
{
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/
var r = str.match(reg)
if(r==null)return false
r[2]=r[2]-1
var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6])
if(d.getFullYear()!=r[1])return false
if(d.getMonth()!=r[2])return false
if(d.getDate()!=r[3])return false
if(d.getHours()!=r[4])return false
if(d.getMinutes()!=r[5])return false
if(d.getSeconds()!=r[6])return false
return true
}
//+---------------------------------------------------
//| 把日期分割成数组
//+---------------------------------------------------
Date.prototype.toArray = function()
{
var myDate = this
var myArray = Array()
myArray[0] = myDate.getFullYear()
myArray[1] = myDate.getMonth()
myArray[2] = myDate.getDate()
myArray[3] = myDate.getHours()
myArray[4] = myDate.getMinutes()
myArray[5] = myDate.getSeconds()
return myArray
}
//+---------------------------------------------------
//| 取得日期数据信息
//| 参数 interval 表示数据类型
//| y 年 m月 d日 w星期 ww周 h时 n分 s秒
//+---------------------------------------------------
Date.prototype.DatePart = function(interval)
{
var myDate = this
var partStr=''
var Week = ['日','一','二','三','四','五','六']
switch (interval)
{
case 'y' :partStr = myDate.getFullYear()break
case 'm' :partStr = myDate.getMonth()+1break
case 'd' :partStr = myDate.getDate()break
case 'w' :partStr = Week[myDate.getDay()]break
case 'ww' :partStr = myDate.WeekNumOfYear()break
case 'h' :partStr = myDate.getHours()break
case 'n' :partStr = myDate.getMinutes()break
case 's' :partStr = myDate.getSeconds()break
}
return partStr
}
TO_DATE格式(以时间:2007-11-02 13:45:25为例)
Year:
yy two digits 两位年显示值:07
yyy three digits 三位年显示值:007
yyyy four digits 四位年显示值:2007
Month:
mmnumber 两位月 显示值:11
monabbreviated 字符集表示 显示值:11月,若是英文版,显示nov
month spelled out 字符集表示 显示值:11月,若是英文版,显示november
Day:
ddnumber 当月第几天显示值:02
dddnumber 当年第几天显示值:02
dyabbreviated 当周第几天简写显示值:星期五,若是英文版,显示fri
dayspelled out 当周第几天全写显示值:星期五,若是英文版,显示friday
ddspth spelled out, ordinal twelfth
Hour:
hhtwo digits 12小时进制显示值:01
hh24 two digits 24小时进制显示值:13
Minute:
mitwo digits 60进制显示值:45
Second:
sstwo digits 60进制显示值:25
其它
Q digit 季度 显示值:4
WWdigit 当年第几周显示值:44
Wdigit 当月第几周显示值:1
24小时格式下时间范围为: 0:00:00 - 23:59:59....
12小时格式下时间范围为: 1:00:00 - 12:59:59 ....
1. 日期和字符转换函数用法(to_date,to_char)
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual //日期转化为字符串
select to_char(sysdate,'yyyy') as nowYear from dual //获取时间的年
select to_char(sysdate,'mm')as nowMonth from dual //获取时间的月
select to_char(sysdate,'dd')as nowDayfrom dual //获取时间的日
select to_char(sysdate,'hh24') as nowHour from dual //获取时间的时
select to_char(sysdate,'mi')as nowMinute from dual //获取时间的分
select to_char(sysdate,'ss')as nowSecond from dual //获取时间的秒
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')from dual//
2.
select to_char( to_date(222,'J'),'Jsp') from dual
显示Two Hundred Twenty-Two
3.求某天是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual
星期一
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual
monday
设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'
也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')
4. 两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual
5. 时间为null的用法
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual
注意要用TO_DATE(null)
6.月份差
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。
所以,当时间需要精确的时候,觉得to_char还是必要的
7. 日期格式冲突问题
输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中写
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual
注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,
可查看
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
8.
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )
查找2002-02-28至2002-02-01间除星期一和七的天数
在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒).
9. 查找月份
select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL
1.03225806451613
10. Next_day的用法
Next_day(date, day)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D
11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第一条记录的TIME 与最后一行是一样的
可以建立一个函数来处理这个问题
create or replace function sys_date return date is
begin
return sysdate
end
select to_char(sys_date,'hh:mi:ss') from all_objects
12.获得小时数
extract()找出日期或间隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL>select sysdate ,to_char(sysdate,'hh') from dual
SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07
SQL>select sysdate ,to_char(sysdate,'hh24') from dual
SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
13.年月日的处理
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days
from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from (
select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp
)
)
14.处理月份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual
16.找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual
闰年的处理方法
to_char( last_day( to_date('02'| | :year,'mmyyyy') ), 'dd' )
如果是28就不是闰年
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)