卫星形式“VB 克隆”中的 TDateTime 到 ShortDateString

发布于 2024-11-02 11:23:02 字数 237 浏览 1 评论 0原文

我试图找到一种计算方法,将 TDateTime 值 40653.6830593 转换为年、月、日、小时、分钟和秒。

当然,我确信这是可能的,但我的大脑似乎没有能力编写一个公式来从该双精度值中提取这些值。

我使用的是 Satellite Forms,一种类似 vb 的语言,但我不需要任何特定的 .NET 库来完成此操作,对吧?它应该只是一个数字计算......对吧?

感谢您的帮助!最真诚的。

I am trying to find a calculation that will convert the TDateTime value 40653.6830593 into a year, a month, a day, an hour, a minute and a second.

I am sure this is possible, of course, but my brain doesn't have the power, it seems, to write a formula that will extract those values from that double.

I am using a Satellite Forms, a vb-like language, but don't expect that I would need any specific .NET libraries to do this, right? It should just be a numeric calculation... right?

Thanks for the help! Most sincerely.

Joe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-11-09 11:23:02

基于您的“Delphi”标签...

在 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 下发布,没有废话)
您可以在以下位置找到:http://www.koders.com/delphi/fidF6715D3FD1D4A92BA7F29F96643D8E9D11C1089F.aspx?s=hook

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;

Based on your 'Delphi' tag...

In 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.

BTW: VB or Delphi, which is it?

If you want to roll your own

From the sysutils unit (released under a dual license GPL2/Borland No nonsense)
Which you can find at: http://www.koders.com/delphi/fidF6715D3FD1D4A92BA7F29F96643D8E9D11C1089F.aspx?s=hook

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;
凹づ凸ル 2024-11-09 11:23:02

旧版 VB 或 VBA(甚至 Excel)会将日期视为自 1899 年 12 月 30 日以来的天数,我相信 Delphi 也是如此。因此,在旧版 VB/VBA 中,您可以编写

Dim myDate as Date
myDate = myDate + 40653.68303593

To get April 20, 2011 4:23:36 PM。在 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...部分天。

Legacy VB or VBA (even Excel) would treat a Date as the number of days since 12/30/1899, and I believe the same is true for Delphi. As such, in legacy VB/VBA, you could write

Dim myDate as Date
myDate = myDate + 40653.68303593

To get April 20, 2011 4:23:36 PM. In VB.NET, it's different, as the DateTime struct defaults to January 1, 0001.

Dim myDate As New DateTime(1899, 12, 30)
myDate = myDate.AddDays(40653.68303593)

Another user has already posted a Delphi answer.

At any rate, the basic premise is that you're adding the number of days, with the decimal portion representing the time. So in this example, 40653 full days have passed since 12/30/1899, and 0.683... partial days.

安稳善良 2024-11-09 11:23:02

要提取小时:

记住 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;

希望有帮助

To extract Hours:

remember 1.0 = 1 day = 24 hours, thus

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;

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文