TDateTime 値 40653.6830593 を年、月、日、時、分、秒に変換する計算を見つけようとしています。
もちろん、これは可能だと確信していますが、私の脳には、その double からこれらの値を抽出する式を書く力がないようです。
vb に似た言語であるサテライト フォームを使用していますが、これを行うために特定の .NET ライブラリが必要になるとは思いませんよね? 数値計算だけのはず…ですよね?
助けてくれてありがとう!誠心誠意。
ジョー
TDateTime 値 40653.6830593 を年、月、日、時、分、秒に変換する計算を見つけようとしています。
もちろん、これは可能だと確信していますが、私の脳には、その double からこれらの値を抽出する式を書く力がないようです。
vb に似た言語であるサテライト フォームを使用していますが、これを行うために特定の .NET ライブラリが必要になるとは思いませんよね? 数値計算だけのはず…ですよね?
助けてくれてありがとう!誠心誠意。
ジョー
「Delphi」タグに基づいて...
デルフィで
implementation
uses DateUtils;
....
var
MyDate: TDateTime;
...other vars...
begin
MyDate:= double(40653.6830593);
MyYear:= YearOf(MyDate);
MyMonth:= MonthOf(MyDate);
MyDay:= DayOf(MyDate);
MyHour:= HourOf(MyDate);
MyMinute:= ... ah well you get the idea.
ところで: VB と Delphi のどちらですか?
自分で巻きたいなら
sysutils ユニットから (デュアル ライセンス GPL2/Borland の下でリリースされて
います。ナンセンスではありません) 次の場所にあります。
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
const
D1 = 365;
D4 = D1 * 4 + 1;
D100 = D4 * 25 - 1;
D400 = D100 * 4 + 1;
var
Y, M, D, I: Word;
T: Integer;
DayTable: PDayTable;
begin
T := DateTimeToTimeStamp(DateTime).Date;
if T <= 0 then
begin
Year := 0;
Month := 0;
Day := 0;
DOW := 0;
Result := False;
end else
begin
DOW := T mod 7 + 1;
Dec(T);
Y := 1;
while T >= D400 do
begin
Dec(T, D400);
Inc(Y, 400);
end;
DivMod(T, D100, I, D);
if I = 4 then
begin
Dec(I);
Inc(D, D100);
end;
Inc(Y, I * 100);
DivMod(D, D4, I, D);
Inc(Y, I * 4);
DivMod(D, D1, I, D);
if I = 4 then
begin
Dec(I);
Inc(D, D1);
end;
Inc(Y, I);
Result := IsLeapYear(Y);
DayTable := @MonthDays[Result];
M := 1;
while True do
begin
I := DayTable^[M];
if D < I then Break;
Dec(D, I);
Inc(M);
end;
Year := Y;
Month := M;
Day := D + 1;
end;
end;
function IsLeapYear(Year: Word): Boolean;
begin
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;
function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
var
I: Integer;
DayTable: PDayTable;
begin
Result := False;
DayTable := @MonthDays[IsLeapYear(Year)];
if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
(Day >= 1) and (Day <= DayTable^[Month]) then
begin
for I := 1 to Month - 1 do Inc(Day, DayTable^[I]);
I := Year - 1;
Date := I * 365 + I div 4 - I div 100 + I div 400 + Day - DateDelta;
Result := True;
end;
end;
従来の VB または VBA (Excel でさえも) は、Date を 1899 年 12 月 30 日からの日数として扱います。Delphi についても同じことが当てはまると思います。そのため、従来の VB/VBA では、次のように記述できます。
Dim myDate as Date
myDate = myDate + 40653.68303593
2011 年 4 月 20 日午後 4 時 23 分 36 秒を取得するには、VB.NET では、DateTime 構造体のデフォルトが 0001 年 1 月 1 日であるため、異なります。
Dim myDate As New DateTime(1899, 12, 30)
myDate = myDate.AddDays(40653.68303593)
別のユーザーがすでに Delphi の回答を投稿しています。
いずれにせよ、基本的な前提は、日数を加算し、小数部分が時間を表すことです。したがって、この例では、1899 年 12 月 30 日から丸 40653 日が経過し、残りの 0.683 日が経過しています。
時間を抽出するには:
1.0 =1日=24時間、つまり
DayPart:= double(MyDateTime) - Floor(double(MyDateTime));
//there is a function for fraction, but I forgot the name,
//never use that stuff.
//if we want to round 0.9999999999 up from 23:59:59 to 24:00:00, do this:
if (RoundTimeUpByHalfASecond = true) then DayPart:= DayPart + (1/(24*60*60*2));
hours:= floor(DayPart*24);
minutes:= floor(DayPart*24*60) mod 60;
seconds:= minutes:= floor(DayPart*24*60*60) mod 60;
お役に立てば幸い