📄 xdatetime.pas
字号:
unit xDateTime;
interface
uses SysUtils;
type
TDateFormatType = set of (dfNormal,dfChinese,dfNoFormat,dfYearAndMonth,dfMonthAndDay,
tfNormal,tfLong,tfChinese,tfNoFormat,tfExact,tfAccumulate);
//------------------------------------------------------------------//
function GetYearString(dt: TDateTime ; bPad : Boolean = True):String;
function GetMonthString(dt: TDateTime ; bPad : Boolean = True):String;
function GetDayString(dt: TDateTime ; bPad : Boolean = True):String;
//------------------------------------------------------------------//
function GetYear(dt: TDateTime):Integer;
function GetMonth(dt: TDateTime):Integer;
function GetDay(dt: TDateTime):Integer;
//------------------------------------------------------------------//
function GetHourString(dt: TDateTime ; bPad : Boolean = True): String;
function GetMinuteString(dt: TDateTime ; bPad : Boolean = True): String;
function GetSecondString(dt: TDateTime ; bPad : Boolean = True): String;
function GetMSecondString(dt: TDateTime ; bPad : Boolean = True): String;
//------------------------------------------------------------------//
function GetHour(dt: TDateTime): Integer;
function GetMinute(dt: TDateTime): Integer;
function GetSecond(dt: TDateTime): Integer;
function GetMSecond(dt: TDateTime): Integer;
//------------------------------------------------------------------//
function GetBeginOfYear(dt: TDateTime): TDateTime;
function GetEndOfYear(dt: TDateTime): TDateTime;
function GetBeginOfMonth(dt: TDateTime): TDateTime;
function GetEndOfMonth(dt: TDateTime): TDateTime;
//------------------------------------------------------------------//
function GetPrevYeard(dt: TDateTime): TDateTime;
function GetNextYeard(dt: TDateTime): TDateTime;
function GetPrevMonthd(dt: TDateTime): TDateTime;
function GetNextMonthd(dt: TDateTime): TDateTime;
//------------------------------------------------------------------//
function GetDateString(dt: TDateTime ; df : TDateFormatType = [dfNormal]):String;
function GetTimeString(dt: TDateTime ; df : TDateFormatType = [tfNormal]):String;
//------------------------------------------------------------------//
function IsLeapYear(dt: TDateTime): Boolean;
function DaysInMonth(dt: TDateTime): Integer;
function SetDate(s:String): TDateTime;
//------------------------------------------------------------------//
function AddTimeStamp(const s: string): string;
function TimeTtoTDateTime(TimeT: Integer): TDateTime;
function TimeToSecond(const hour, minute, sec: Integer): Integer;
procedure SecondToTime(const secs: Integer; var hour, minute, sec: Word);
function SecondToTimeStr(secs: Integer): string;
var
nYear, nMonth, nDay: Word;
sYear, sMonth, sDay: String;
nHour, nMinute, nSecond, nMSecond: Word;
sHour, sMinute, sSecond, sMSecond: String;
implementation
uses xStrings;
//------------------------------------------------------------------//
//取得年份字符串
function GetYearString(dt: TDateTime ; bPad : Boolean = True):String;
begin
DecodeDate(dt, nYear, nMonth, nDay);
if bPad then Result:=PadL(IntToStr(nYear),4,'0')
else Result:=IntToStr(nYear);
end;
//------------------------------------------------------------------//
//取得月份字符串
function GetMonthString(dt: TDateTime ; bPad : Boolean = True):String;
begin
DecodeDate(dt, nYear, nMonth, nDay);
if bPad then Result:=PadL(IntToStr(nMonth),2,'0')
else Result:=IntToStr(nMonth);
end;
//------------------------------------------------------------------//
//取得第几日字符串
function GetDayString(dt: TDateTime ; bPad : Boolean = True):String;
begin
DecodeDate(dt, nYear, nMonth, nDay);
if bPad then Result:=PadL(IntToStr(nDay),2,'0')
else Result:=IntToStr(nDay);
end;
//------------------------------------------------------------------//
//取得年
function GetYear(dt: TDateTime):Integer;
begin
DecodeDate(dt, nYear, nMonth, nDay);
Result:=nYear;
end;
//------------------------------------------------------------------//
//取得月
function GetMonth(dt: TDateTime):Integer;
begin
DecodeDate(dt, nYear, nMonth, nDay);
Result:=nMonth;
end;
//------------------------------------------------------------------//
//取得日
function GetDay(dt: TDateTime):Integer;
begin
DecodeDate(dt, nYear, nMonth, nDay);
Result:=nDay;
end;
//------------------------------------------------------------------//
//取得小时字符串
function GetHourString(dt: TDateTime ; bPad : Boolean = True): String;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
if bPad then Result:=PadL(IntToStr(nHour),2,'0')
else Result:=IntToStr(nHour);
end;
//------------------------------------------------------------------//
//取得分钟字符串
function GetMinuteString(dt: TDateTime ; bPad : Boolean = True): String;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
if bPad then Result:=PadL(IntToStr(nMinute),2,'0')
else Result:=IntToStr(nMinute);
end;
//------------------------------------------------------------------//
//取得秒字符串
function GetSecondString(dt: TDateTime ; bPad : Boolean = True): String;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
if bPad then Result:=PadL(IntToStr(nSecond),2,'0')
else Result:=IntToStr(nSecond);
end;
//------------------------------------------------------------------//
//取得毫秒字符串
function GetMSecondString(dt: TDateTime ; bPad : Boolean = True): String;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
if bPad then Result:=PadL(IntToStr(nMSecond),2,'0')
else Result:=IntToStr(nMSecond);
end;
//------------------------------------------------------------------//
//取得时数
function GetHour(dt: TDateTime): Integer;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
Result:=nHour;
end;
//------------------------------------------------------------------//
//取得分
function GetMinute(dt: TDateTime): Integer;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
Result:=nMinute;
end;
//------------------------------------------------------------------//
//取得秒
function GetSecond(dt: TDateTime): Integer;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
Result:=nSecond;
end;
//------------------------------------------------------------------//
//取得毫秒
function GetMSecond(dt: TDateTime): Integer;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
Result:=nHour;
end;
//------------------------------------------------------------------//
//一年的第一天
function GetBeginOfYear(dt: TDateTime): TDateTime;
begin
DecodeDate(dt,nYear,nMonth,nDay);
Result:=EncodeDate(nYear,1,1);
end;
//------------------------------------------------------------------//
//一年的最后一天
function GetEndOfYear(dt: TDateTime): TDateTime;
begin
DecodeDate(dt,nYear,nMonth,nDay);
Result:=EncodeDate(nYear,12,31);
end;
//------------------------------------------------------------------//
//一个月的第一天
function GetBeginOfMonth(dt: TDateTime): TDateTime;
begin
DecodeDate(dt,nYear,nMonth,nDay);
Result:=EncodeDate(nYear,nMonth,1);
end;
//------------------------------------------------------------------//
//一个月的最后一天
function GetEndOfMonth(dt: TDateTime): TDateTime;
begin
DecodeDate(dt,nYear,nMonth,nDay);
if nMonth=12 then
begin
Inc(nYear);
nMonth:=1;
end else
Inc(nMonth);
Result:=EncodeDate(nYear,nMonth,1)-1;
end;
//------------------------------------------------------------------//
//上一年最后一天
function GetPrevYeard(dt: TDateTime): TDateTime;
begin
Result:=GetBeginOfYear(dt)-1;
end;
//------------------------------------------------------------------//
//下一年第一天
function GetNextYeard(dt: TDateTime): TDateTime;
begin
Result:=GetEndOfYear(dt)+1;
end;
//------------------------------------------------------------------//
//上一月最后一天
function GetPrevMonthd(dt: TDateTime): TDateTime;
begin
Result:=GetBeginOfMonth(dt)-1;
end;
//------------------------------------------------------------------//
//下一月第一天
function GetNextMonthd(dt: TDateTime): TDateTime;
begin
Result:=GetEndOfMonth(dt)+1;
end;
//------------------------------------------------------------------//
//取得日期字符串,格式由df定义
function GetDateString(dt: TDateTime ; df : TDateFormatType = [dfNormal]):String;
begin
DecodeDate(dt, nYear, nMonth, nDay);
sYear:=PadL(IntToStr(nYear),4,'0');
sMonth:=PadL(IntToStr(nMonth),2,'0');
sDay:=PadL(IntToStr(nDay),2,'0');
Result:=sYear+'.'+sMonth+'.'+sDay; //default
if df = [dfNormal] then Result:=sYear+'.'+sMonth+'.'+sDay;
if df = [dfChinese] then Result:=sYear+'年'+sMonth+'月'+sDay+'日';
if df = [dfNoFormat] then Result:=sYear+sMonth+sDay;
if df = [dfYearAndMonth] then Result:=sYear+'.'+sMonth;
if df = [dfMonthAndDay] then Result:=sMonth+','+sDay;
if df = [dfNoFormat,dfYearAndMonth] then Result:=sYear+sMonth;
if df = [dfNoFormat,dfMonthAndDay] then Result:=sMonth+sDay;
if df = [dfYearAndMonth,dfChinese] then Result:=sYear+'年'+sMonth+'月';
if df = [dfMonthAndDay,dfChinese] then Result:=sMonth+'月'+sDay+'日';
end;
//------------------------------------------------------------------//
//取得时间字符串,格式由df定义
function GetTimeString(dt: TDateTime ; df : TDateFormatType = [tfNormal]):String;
var
sLocal: String;
begin
DecodeTime(dt, nHour, nMinute, nSecond, nMSecond);
if df = [tfAccumulate] then //计时还是累积时间。计时不使用前缀零。
begin
sLocal := '小时';
sHour := IntToStr(nHour);
sMinute := IntToStr(nMinute);
sSecond := IntToStr(nSecond);
sMSecond:= IntToStr(nMSecond);
end
else
begin
sLocal := '时';
sHour := PadL(IntToStr(nHour),2,'0');
sMinute := PadL(IntToStr(nMinute),2,'0');
sSecond := PadL(IntToStr(nSecond),2,'0');
sMSecond:= PadL(IntToStr(nMSecond),2,'0');
end;
Result:=sHour+':'+sMinute+':'+sSecond; //default
if df = [tfNormal] then Result:=sHour+':'+sMinute;
if df = [tfLong] then Result:=sHour+':'+sMinute+':'+sSecond;
if df = [tfExact] then Result:=sHour+':'+sMinute+':'+sSecond+' '+sMSecond;
if df = [tfNoFormat] then Result:=sHour+sMinute;
if df = [tfChinese] then Result:=sHour+sLocal+sMinute+'分';
if df = [tfLong ,tfChinese] then Result:=sHour+sLocal+sMinute+'分'+sSecond+'秒';
if df = [tfExact ,tfChinese] then Result:=sHour+sLocal+sMinute+'分'+sSecond+'秒'+sMSecond;
if df = [tfNormal,tfChinese] then Result:=sHour+sLocal+sMinute+'分';
if df = [tfNormal,tfNoFormat] then Result:=sHour+sMinute;
if df = [tfLong ,tfNoFormat] then Result:=sHour+sMinute+sSecond;
if df = [tfExact ,tfNoFormat] then Result:=sHour+sMinute+sSecond+sMSecond;
end;
function IsLeapYear(dt: TDateTime): Boolean;
begin
nYear := GetYear(dt);
Result := (nYear mod 4 = 0) and ((nYear mod 100 <> 0) or (nYear mod 400 = 0))
end;
//------------------------------------------------------------------//
//计算给定月份的天数
function DaysInMonth(dt: TDateTime): Integer;
const
TDaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
nMonth := GetMonth(dt);
if (nMonth = 2) and IsLeapYear(dt) then Result := 29 else Result := TDaysInMonth[nMonth];
end;
//------------------------------------------------------------------//
//设置日期,失败时返回当前日期
function SetDate(s:String): TDateTime;
begin
sYear := LeftString(s,4);
sMonth:= SubString(s,6,2);
sDay := RightString(s,2);
Result:= Now;
if not (IsNumberString(sYear) and IsNumberString(sMonth) and IsNumberString(sDay)) then Exit;
nYear := StrToInt(sYear);
nMonth:= StrToInt(sMonth);
nDay := StrToInt(sDay);
EncodeDate(nYear,nMonth,nDay);
end;
//------------------------------------------------------------------//
//为字符串加上时间戳
function AddTimeStamp(const S: string): string;
var
st:string;
begin
st:=GetDateString(Date)+' '+GetTimeString(Date);
if S = '' then
Result := st
else if S[Length(S)] = #10 then
Result := Copy(S, 1, Length(S) - 2) + ' ' + st + #13#10
else
Result := S + ' ' + st;
end;
//------------------------------------------------------------------//
//C++的time_t格式转换为Delphi的TDateTime格式。
function TimeTtoTDateTime(TimeT: Integer): TDateTime;
var
ts: TTimeStamp;
begin
Dec(TimeT, 3600 * 8); // still unprecise
ts.Time := (TimeT mod 86400) * 1000;
ts.Date := TimeT div 86400 + 719163;
Result := TimeStampToDateTime(ts);
end;
//------------------------------------------------------------------//
//将取得的时、分、秒转换为秒。
function TimeToSecond(const hour, minute, sec: Integer): Integer;
begin
Result := hour * 3600 + minute * 60 + sec;
end;
//------------------------------------------------------------------//
//将取得秒转换为时、分、秒。
procedure SecondToTime(const secs: Integer; var hour, minute, sec: Word);
begin
hour := secs div 3600;
minute := (secs mod 3600) div 60;
sec := secs mod 60;
end;
//------------------------------------------------------------------//
//将取得的秒转换为时、分、秒字符串。
function SecondToTimeStr(secs: Integer): string;
var
H, M, S: Word;
begin
SecondToTime(secs, h, m, s);
result := '';
if h <> 0 then Result := result + format('%-.2d 小时 ', [h]);
if m <> 0 then Result := result + format('%-.2d 分 ', [m]);
if s <> 0 then Result := result + format('%-.2d 秒 ', [s]);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -