您的位置:首页 > Web前端 > JavaScript

javascript处理时间的函数【转载】

2009-09-18 17:27 519 查看
//----------------------------------------------------------------------
this.DateAdd= function(part,n,datetime){//模拟sqlserver中的dateadd函数
/*
year yy, yyyy 年
month mm, m 月
day dd, d 日
week wk, ww 周
hour hh 时
minute mi, n 分
second ss, s 秒
*/
var DateArray = new Array();
DateArray = this.SplitDate(datetime);
var tmpYear = this.GetFormatYear(DateArray[0]);
var tmpMonth = DateArray[1];
var tmpDay = DateArray[2];
var tmpHour = DateArray[3];
var tmpMinute = DateArray[4];
var tmpSecond = DateArray[5];
part=part.toLowerCase();
switch(part)
{
case "year":
case "yy":
case "yyyy":
tmpYear+=n;
break;
case "month":
case "mm":
case "m":
tmpMonth+=n;
break;
case "day":
case "dd":
case "d":
tmpDay+=n;
break;
case "week":
case "wk":
case "ww":
tmpDay+=7*n;
break;
case "hour":
case "hh":
tmpHour+=n;
break;
case "minute":
case "ni":
case "n":
tmpMinute+=n;
break;
case "second":
case "ss":
case "s":
tmpSecond+=n;
break;
}
var date= new Date(tmpYear, tmpMonth,tmpDay,tmpHour,tmpMinute,tmpSecond);
return this.SetDateFormat(date);

}
//----------------------------------------------------------------------

this.DateDiff = function(part,startdate,enddate){//模拟sqlserver中的datediff函数
/*
year yy, yyyy 年
month mm, m 月
day dd, d 日
week wk, ww 周
hour hh 时
minute mi, n 分
second ss, s 秒
*/
var DAS = new Array();
var DAE = new Array();
DAS = this.SplitDate(startdate);
DAE = this.SplitDate(enddate);
var tmpYearS = this.GetFormatYear(DAS[0]);
var tmpMonthS = DAS[1];
var tmpDayS = DAS[2];
var tmpHourS = DAS[3];
var tmpMinuteS = DAS[4];
var tmpSecondS = DAS[5];
var tmpTimeStampS = DAS[6];

var tmpYearE = this.GetFormatYear(DAE[0]);
var tmpMonthE = DAE[1];
var tmpDayE = DAE[2];
var tmpHourE = DAE[3];
var tmpMinuteE = DAE[4];
var tmpSecondE = DAE[5];
var tmpTimeStampE = DAE[6];

var ret=0 ; //差值
part=part.toLowerCase();

switch(part)
{
case "year":
case "yy":
case "yyyy":
ret=tmpYearE-tmpYearS;
break;
case "month":
case "mm":
case "m":
ret=(tmpYearE-tmpYearS)*12+tmpMonthE-tmpMonthS;
break;
case "day":
case "dd":
case "d":
ret=(tmpTimeStampE-tmpTimeStampS)/(24 * 60 * 60 * 1000);
break;
case "week":
case "wk":
case "ww":
ret=(tmpTimeStampE-tmpTimeStampS)/(7 * 24 * 60 * 60 * 1000);
break;
case "hour":
case "hh":
ret=(tmpTimeStampE-tmpTimeStampS)/(60 * 60 * 1000);
break;
case "minute":
case "ni":
case "n":
ret=(tmpTimeStampE-tmpTimeStampS)/(60 * 1000);
break;
case "second":
case "ss":
case "s":
ret=(tmpTimeStampE-tmpTimeStampS)/(1000);
break;
}
return ret.toFixed(0);
}
}
function DateFunction(oDateTime)
{
this.MinYear = 1970; //最大年份
this.MaxYear = 2030; //最小年份
this.DateFormat = "<F>"; //要输出时间的格式
//<yy> <yyyy>代表年, <m> <mm>代表月, <d> <dd>代表天<h> <hh>代表小时<n> <nn>代表分<s> <ss>代表秒
//for example:
// <yyyy>-<mm>-<dd> : 2002-04-01
// <yy>.<m>.<d> : 02.4.1
// <m>/<d>/<yy> : 4/1/02
// <dd>/<mm>/<yyyy> : 01/04/2002
// <hh>:<nn>:<ss> : 01:01:04
// <F>: 2002-04-01 01:01:04
// <S>: 2002-04-01
// <t>: 01:01:04
// <T>: 返回时间戳
this.GetDateTime=function(oDateTime) {//按照指定格式返回时间
return this.SetDateFormat(oDateTime);
}
this.Now=function(){//返回当前时间
var date=new Date()
return this.SetDateFormat(date);
}

this.SplitDate = function(oDateTime){//拆分时间,返回一个按照年月日时分秒排列的数组
var z1, z2, z3, z4, z5
if (isNaN(Date.parse(oDateTime)))
{
if (oDateTime.indexOf("-")!=-1)
{
z1 = oDateTime.indexOf("-");
z2 = oDateTime.indexOf("-", z1+1);
}
else if(oDateTime.indexOf("/")!=-1)
{
z1 = oDateTime.indexOf("/");
z2 = oDateTime.indexOf("/", z1+1);
}
szYear = Number(oDateTime.slice(0, z1));
szMonth = Number(oDateTime.slice(z1+1,z2)) -1 ;

if (oDateTime.indexOf(":")!=-1)
{
z3 = oDateTime.indexOf(" ", z2+1);
z4 = oDateTime.indexOf(":", z3+1);
z5 = oDateTime.indexOf(":", z4+1);
szDate = Number(oDateTime.slice(z2+1,z3)) ;
szHour = Number(oDateTime.slice(z3+1,z4));
szMinute = Number(oDateTime.slice(z4+1,z5));
szSecond = Number(oDateTime.slice(z5+1));
}
else
{
szDate = Number(oDateTime.slice(z2+1)) ;
szHour = Number(0);
szMinute = Number(0);
szSecond = Number(0);
}
}
else
{
szYear = oDateTime.getYear();
szMonth =oDateTime.getMonth();
szDate = oDateTime.getDate();
szHour = oDateTime.getHours() ;
szMinute =oDateTime.getMinutes() ;
szSecond = oDateTime.getSeconds();
}
var szTimeStamp=new Date(szYear,szMonth,szDate,szHour,szMinute,szSecond).valueOf();

var DateArray = new Array(szYear,szMonth,szDate,szHour,szMinute,szSecond,szTimeStamp);
return DateArray;
}
//---------------------------------------------------------------------
this.GetFormatYear = function(theYear){//把年份格式化成4位
var tmpYear = parseInt(theYear,10);
if (tmpYear < 100){
tmpYear += 1900;
if (tmpYear < 1940){
tmpYear += 100;
}
}
if (tmpYear < this.MinYear){
tmpYear = this.MinYear;
}
if (tmpYear > this.MaxYear){
tmpYear = this.MaxYear;
}
return(tmpYear);
}

//---------------------------------------------------------------------
this.GetMonthDays = function(theYear, theMonth){ //获得当年当月最大天数
var theDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var theMonthDay = 0, tmpYear = this.GetFormatYear(theYear);
theMonthDay = theDays[theMonth];
if (theMonth == 1){ //theMonth is February
if (((tmpYear % 4 == 0) && (tmpYear % 100 != 0)) || (tmpYear % 400 == 0)){
theMonthDay++;
}
}
return(theMonthDay);
}
//----------------------------------------------------------------------
this.SetDateFormat = function(oDateTime){//按照dateformat格式化时间
var theDate = this.DateFormat;
var DateArray = new Array();
DateArray = this.SplitDate(oDateTime);

var tmpYear = this.GetFormatYear(DateArray[0]);
var tmpMonth = DateArray[1];
var tmpDay = DateArray[2];
var tmpHour = DateArray[3];
var tmpMinute = DateArray[4];
var tmpSecond = DateArray[5];
if (tmpMonth < 0){
tmpMonth = 0;
}
if (tmpMonth > 11){
tmpMonth = 11;
}
if (tmpDay < 1){
tmpDay = 1;
}else{
tmpDay = this.GetMonthDays(tmpYear, tmpMonth);
if (DateArray[2] < tmpDay){
tmpDay = DateArray[2];
}
}
if (theDate.indexOf("<T>")!=-1)
{
theDate = new Date(tmpYear, tmpMonth,tmpDay,tmpHour,tmpMinute,tmpSecond).valueOf();
}
else if (theDate.indexOf("<t>")!=-1)
{
theDate = tmpHour.toString()+":"+tmpMinute.toString()+":"+tmpSecond.toString();
}
else if (theDate.indexOf("<F>")!=-1)
{
theDate = tmpYear.toString()+"-"+(tmpMonth + 1).toString()+"-"+tmpDay.toString()+" "+tmpHour.toString()+":"+tmpMinute.toString()+":"+tmpSecond.toString();

}
else if (theDate.indexOf("<S>")!=-1)
{
theDate = tmpYear.toString()+"-"+(tmpMonth + 1).toString()+"-"+tmpDay.toString();
}
else
{
theDate = theDate.replace(/<yyyy>/g, tmpYear.toString());
theDate = theDate.replace(/<yy>/g, tmpYear.toString().substr(2,2));
if (DateArray[1] < 9){
theDate = theDate.replace(/<mm>/g, "0" + (tmpMonth + 1).toString());
}else{
theDate = theDate.replace(/<mm>/g, (tmpMonth + 1).toString());
}
theDate = theDate.replace(/<m>/g, (tmpMonth + 1).toString());
if (DateArray[2] < 10){
theDate = theDate.replace(/<dd>/g, "0" + tmpDay.toString());
}else{
theDate = theDate.replace(/<dd>/g, tmpDay.toString());
}
theDate = theDate.replace(/<d>/g, tmpDay.toString());

if (DateArray[3] < 10){
theDate = theDate.replace(/<hh>/g, "0" + tmpHour.toString());
}else{
theDate = theDate.replace(/<hh>/g, tmpHour.toString());
}
theDate = theDate.replace(/<h>/g, tmpHour.toString());

if (DateArray[4] < 10){
theDate = theDate.replace(/<nn>/g, "0" + tmpMinute.toString());
}else{
theDate = theDate.replace(/<nn>/g, tmpMinute.toString());
}
theDate = theDate.replace(/<n>/g, tmpMinute.toString());

if (DateArray[5] < 10){
theDate = theDate.replace(/<ss>/g, "0" + tmpSecond.toString());
}else{
theDate = theDate.replace(/<ss>/g, tmpSecond.toString());
}
theDate = theDate.replace(/<s>/g, tmpSecond.toString());
}

return(theDate);
}
/*****************************************************************
* 文件名称: FunctionForDatetime.js
* 功能说明: 时间函数增强
* 作者姓名: qqdao
* E - MAIL: qqdao@263.net
* 创建日期: 2002-11-18 12:39下午
* 版本:1.00
* 兼容:IE5.0 6.0
|****************************************************************|
版权声明:
时间函数增强脚本组件 DateFunction,为
[青青岛(qqdao){E-mail:qqdao@263.net MSN :qqdao@hotmail.com OICQ:195188}]
所编写。所有版权保留。

在非商业用途下,任何个人或组织能够对此代码进行修改、复制、传播,和使用。
在没有经过作者的允许下,任何人或组织不得利用这个代码及基于这个代码的修改版进行任何商业用途。
******************************************************************
功能说明:
方法:
GetDateTime 按照指定格式返回时间
Now 返回当前时间
SplitDate 拆分时间,返回一个按照年月日时分秒排列的数组
GetFormatYear 把年份格式化成4位
GetMonthDays 获得当年当月最大天数
SetDateFormat 按照dateformat格式化时间
DateAdd 模拟sqlserver中的dateadd函数
DateDiff 模拟sqlserver中的datediff函数
year yy, yyyy 年
month mm, m 月
day dd, d 日
week wk, ww 周
hour hh 时
minute mi, n 分
second ss, s 秒
属性:
MinYear = 1970; //最大年份
MaxYear = 2030; //最小年份
DateFormat = "<yyyy>-<mm>-<dd>"; //要输出时间的格式
//<yy> <yyyy>代表年, <m> <mm>代表月, <d> <dd>代表天<h> <hh>代表小时<n> <nn>代表分<s> <ss>代表秒
//for example:
// <yyyy>-<mm>-<dd> : 2002-04-01
// <yy>.<m>.<d> : 02.4.1
// <m>/<d>/<yy> : 4/1/02
// <dd>/<mm>/<yyyy> : 01/04/2002
// <hh>:<nn>:<ss> : 01:01:04
// <F>: 2002-04-01 01:01:04
// <S>: 2002-04-01
// <t>: 01:01:04
// <T>: 返回时间戳
******************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: