网友很好用的日期函数
//函数名称:f_dateadd()//参数说明:as_datepart string 日期单位,可取如下值:
// year,yyyy,yy,y,年:年;
// quarter,qq,季:季;
// month,mm,m,月:月;
// day,dd,天:天;
// week,wk,ww,星期,周:星期;
// hour,hh,h,小时:小时
// minute,mi,分:分钟;
// second,ss,秒:秒;
// millisecond,ms,毫秒:毫秒
// al_number long 相对数量,取后来日期,为正值, 取前置日期,为负值
// adt_refdate datetime 参考日期
//返回值: datetime 相对数量单位前(后)的日期
// 若三个参数中有一个为空,则返回空值,若输入的日期单位非要求的单位也返回空值
//功能描述:计算指定日期相对数量单位前(后)的日期
//创建日期:2006-07-27
//版本号: V1.0
//*******************************************************************/
//====================================================================
// 事件: .f_dateadd()
//--------------------------------------------------------------------
// 描述:
//--------------------------------------------------------------------
// 参数:
//value string as_datepart
//value long al_number
//value datetime adt_refdate
//--------------------------------------------------------------------
// 返回:datetime
//====================================================================
DateTime ldt_return
String ls_sql
Any la_values[]
Long ll_year,ll_month,ll_day,ll_hour,ll_minute,ll_second,ll_millisecond
Long ll_day_tmp,ll_hour_tmp,ll_minute_tmp,ll_second_tmp,ll_millisecond_tmp
Date ld_return
Time lt_return
//检查参数是否有效
IF IsNull(as_datepart) Or IsNull(al_number) Or IsNull(adt_refdate) THEN
SetNull(ldt_return)
RETURN ldt_return
END IF
IF al_number = 0 THEN RETURN adt_refdate
//取各部分时间
ll_year = Year(Date(adt_refdate))
ll_month = Month(Date(adt_refdate))
ll_day = Day(Date(adt_refdate))
ll_hour = Hour(Time(adt_refdate))
ll_minute = Minute(Time(adt_refdate))
ll_second = Second(Time(adt_refdate))
ll_millisecond = Long(rightw(String(adt_refdate,'yyyy-mm-dd hh:mm:ss.fff'),3))
//根据不同的日期单位进行计算
CHOOSE CASE Lower(as_datepart)
CASE "year","yyyy","yy","y","年" //年
ll_year = ll_year + al_number
IF Not IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day)) THEN
DO WHILE IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
ll_day --
LOOP
END IF
ld_return = Date(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "quarter","qq","q","季" //季
ll_year = ll_year + Long((al_number * 3 + ll_month) / 12) - 1
IF Mod(al_number * 3 + ll_month,12) = 0 THEN
ll_month = 12
ELSE
ll_month = Mod(al_number * 3 + ll_month,12)
END IF
IF Not IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day)) THEN
DO WHILE IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
ll_day --
LOOP
END IF
ld_return = Date(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "month","mm","m","月" //月
IF al_number > 0 THEN
ll_year = ll_year + al_number / 12
ELSE
IF Mod(Abs(al_number),12) = 0 THEN
ll_year = ll_year + al_number / 12
ELSE
ll_year = ll_year + al_number / 12 + 1
END IF
END IF
ll_month = ll_month + Mod(al_number,12)
IF ll_month > 12 THEN
ll_year = ll_year + 1
ll_month = ll_month - 12
ELSEIF ll_month = 0 THEN
ll_year = ll_year - 1
ll_month = 12
END IF
IF Not IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day)) THEN
DO WHILE IsDate(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
ll_day --
LOOP
END IF
ld_return = Date(String(ll_year) + "-" + String(ll_month) + "-" + String(ll_day))
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "day","dd","d","日" //日
ld_return = RelativeDate (Date(adt_refdate),al_number)
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "week","wk","ww","星期","周" //周
RETURN f_dateadd('day',al_number * 7,adt_refdate)
CASE "hour","hh","h","小时" //小时
ld_return = RelativeDate (Date(adt_refdate),Truncate((al_number + ll_hour) / 24,0))
IF al_number > 0 THEN
ll_hour = Mod(al_number + ll_hour,24)
ELSE
ll_hour = 24 - Mod(Abs(al_number + ll_hour),24)
ld_return = RelativeDate (ld_return,-1)
END IF
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "minute","mi","n","分" //分钟
IF ll_hour * 60 + ll_minute + al_number < 0 THEN
ll_day_tmp = Ceiling(Abs(ll_hour * 60 + ll_minute + al_number) / 60 / 24) * (-1)
ELSE
ll_day_tmp = (ll_hour * 60 + ll_minute + al_number) / 60 / 24
END IF
ld_return = RelativeDate (Date(adt_refdate),ll_day_tmp)
ll_hour_tmp = (ll_hour * 60 + ll_minute + al_number - ll_day_tmp * 24 * 60) / 60
ll_minute = ((ll_hour * 60 + ll_minute + al_number - ll_day_tmp * 24 * 60) / 60 - ll_hour_tmp) * 60
ll_hour = ll_hour_tmp
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "second","ss","s","秒" //秒
IF ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number < 0 THEN
ll_day_tmp = Ceiling(Abs(ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number) / 60 / 60 / 24) * (-1)
ELSE
ll_day_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number) / 60 / 60 / 24
END IF
ld_return = RelativeDate (Date(adt_refdate),ll_day_tmp)
ll_hour_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60) / 60 / 60
ll_minute_tmp = (ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60 - ll_hour_tmp * 60 * 60) / 60
ll_second = ll_hour * 60 * 60 + ll_minute * 60 + ll_second + al_number - ll_day_tmp * 24 * 60 * 60 - ll_hour_tmp * 60 * 60 - ll_minute_tmp * 60
ll_hour = ll_hour_tmp
ll_minute = ll_minute_tmp
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE "millisecond","ms","毫秒" //毫秒
//先换算成秒计算
IF ll_millisecond + al_number < 0 THEN
ll_second_tmp = Ceiling(Abs(ll_millisecond + al_number) / 1000) * (-1)
ELSE
ll_second_tmp = (ll_millisecond + al_number) / 1000
END IF
ll_millisecond = ll_millisecond + al_number - ll_second_tmp * 1000
IF ll_millisecond >= 1000 THEN
ll_second_tmp = ll_second_tmp + 1
ldt_return = f_dateadd('second',ll_second_tmp,adt_refdate)
ll_millisecond = 1000 - ll_millisecond
ELSE
ldt_return = f_dateadd('second',ll_second_tmp,adt_refdate)
END IF
ld_return = Date(ldt_return)
ll_hour = Hour(Time(ldt_return))
ll_minute = Minute(Time(ldt_return))
ll_second = Second(Time(ldt_return))
lt_return = Time(String(ll_hour,'00')+":"+String(ll_minute,'00')+":"+String(ll_second,'00')+"."+String(ll_millisecond,'000'))
CASE ELSE //无效参数
SetNull(ldt_return)
RETURN ldt_return
END CHOOSE
ldt_return = DateTime(ld_return,lt_return)
RETURN ldt_return
页:
[1]