Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1201406
  • 博文数量: 398
  • 博客积分: 10110
  • 博客等级: 上将
  • 技术积分: 4055
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-23 20:01
个人简介

新博客http://www.cnblogs.com/zhjh256 欢迎访问

文章分类

全部博文(398)

文章存档

2012年(1)

2011年(41)

2010年(16)

2009年(98)

2008年(142)

2007年(100)

我的朋友

分类: Oracle

2007-12-25 10:45:33

--desc:calculate the number of days,hours,minutes between two dates.
create or replace procedure date_test(start_date date, end_date date) is
  DateLow   date;
  DateHigh  date;
  day1      date;
  day2      date;
  Fraction1 number;
  Fraction2 number;
  days      number;
  HHDec     number; -- Hours (decimal)
  HH        number; -- Hours (integer)
  MMDec     number; -- Minutes (decimal)
  MM        number; -- Minutes (integer)
  SSDec     number; -- Seconds (deminal)
  SS        number; -- Seconds (integrer)
begin
  -- Put the dates in order (important when dealing with the fractions
  -- of days left over from the subtraction - see later).
  DateLow  := least(start_date, end_date);
  DateHigh := greatest(start_date, end_date);
  -- Find the midnight preceding start_date
  Day1 := Trunc(DateLow);
  -- Find the midnight preceding end_date
  Day2 := Trunc(DateHigh);
  -- Calculate the days between the two dates (subtracting two DATEs gives a NUMBER)
  -- A function to calculate the number of days between the two dates would return
  -- this value.
  Days := Day2 - Day1 - 1;
  -- Now, what about the fractions of days left?
  Fraction1 := start_date - Day1;
  Fraction2 := end_date - Day2;
  -- Calculate the total HH (and fractions of HH) from the
  -- fractions of the days left over
  HHDec := ((1 - Fraction1) + Fraction2) * 24;
  -- This could be >1 day
  if HHDec >= 24 then
    HHDec := HHDec - 24;
    Days  := Days + 1;
  end if;
  HH := floor(HHDec);
  -- Here, you have the correct number of days and HH between the two dates.
  -- Now obtain the MM from the fractions of HH.
  MMDec := (HHDec - HH) * 60;
  MM    := floor(MMDec);
  -- And the SS...
  SSDec := (MMDec - MM) * 60;
  SS    := floor(SSDec);
  -- You could carry on from here to get the fractions of a second......
  --
  dbms_output.put_line(to_char(days) || ' Days, ' || to_char(HH) ||
                       ' Hours, ' || to_char(MM) || ' Minutes, ' ||
                       to_char(SS) || ' Seconds. ');
end;
阅读(1110) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~