简体   繁体   English

将 SQL 中的 BigInt 值转换为 c# 中的日期时间

[英]Convert BigInt value in SQL to datetime in c#

I usually use the SQL function below to convert bigint in sql to datetime in SQL Server 2005.我通常使用下面的 SQL function 将 sql 中的 bigint 转换为 Z9778840A0201B5B5A22 服务器中的 datetime 。

DECLARE @datetime AS bigint, @day AS int, @ms AS int
SET @datetime = 129471410567460000 
SET @ms = (@datetime / CAST(10000 AS bigint)) % 86400000
SET @day = @datetime / CAST(864000000000 AS bigint) - 109207
SELECT DATEADD(ms,@ms, DATEADD(day, @day, 0))

How do i do it using c#?我如何使用 c# 做到这一点? I am able to read in time value (129471410567460000) from the SQL server and want to convert to datetime.我能够从 SQL 服务器读取时间值 (129471410567460000) 并希望转换为日期时间。 The datetime will be used in a c# application.日期时间将在 c# 应用程序中使用。

Cheers干杯

Are you trying to convert a bigint in SQL Server to a datetime in C# ?您是否尝试将 SQL 服务器中的 bigint 转换为 C# 中的日期时间 If so, you would do something like:如果是这样,您将执行以下操作:

var tickValue = 129471410567460000;
var datetime = new DateTime( tickValue );

If however, you are tying to convert a bigint value in SQL Server to a datetime value in SQL Server then look at the following link:但是,如果您想将 SQL 服务器中的 bigint 值转换为 SQL 服务器中的日期时间值,请查看以下链接:

Convert .NET Ticks to SQL Server DateTime 将 .NET 刻度转换为 SQL 服务器日期时间

If you are trying mimic your exact logic (how you are getting that tick value is its own mystery):如果您尝试模仿您的确切逻辑(如何获得该刻度值本身就是个谜):

var tickValue = 129471410567460000;
var ms = ( tickValue / 10000 ) % 86400000;
var day = tickValue / 864000000000 - 109207;

var startDate = new DateTime( 1900, 1, 1 );
var resultDateTime = startDate.AddMilliseconds( ms ).AddDays( day );

The trick to this logic is the start date.这个逻辑的诀窍是开始日期。 In SQL Server, day zero = '1900-01-01' even though DateTime values can store values going back to 1753.在 SQL 服务器中,第零天 = '1900-01-01',即使 DateTime 值可以存储回溯到 1753 的值。


In comments, you mentioned that the SQL method was posted in a forum.在评论中,您提到 SQL 方法已发布在论坛中。 It is crucial that you know the method used to calculate the bigint value.了解用于计算 bigint 值的方法至关重要。 The forum seems to suggest that this value is a Win32 FILETIME structure: that stores the date as 100-nanosecond intervals since 1601 .该论坛似乎建议该值是Win32 FILETIME 结构:将日期存储为自 1601 以来的 100 纳秒间隔 If that is the case, the code you would use in C# is:如果是这种情况,您将在 C# 中使用的代码是:

var startDate = new DateTime( 1601, 1, 1 );
var resultDateTime = startDate.AddTicks( tickValue );

You will note that this value returns 2003-05-14 4:51:56 PM which is the approximate date and time of the forum thread.您会注意到,此值返回2003-05-14 4:51:56 PM ,这是论坛主题的大致日期和时间。

Solution is解决方案是

DateTime.FromFileTimeUtc()
public DateTime ConvertLongFormate2DateTime(long iDatelongValue)
{
    try
    {
        var iSecondCal = (iDatelongValue / 1000 + 8 * 60 * 60);
        var startDate = new DateTime(1970, 01, 01);
        DateTime resultDateTime = startDate.AddSeconds(iSecondCal);
        return resultDateTime;
    }
    catch (Exception)
    {
        throw;
    }
}

You could make it a stored procedure, then call that from C#.您可以将其设为存储过程,然后从 C# 调用它。

Try this:尝试这个:

long datetime = 129471410567460000;
int ms = (int)((datetime / 10000) % 86400000);
int day = (int)(datetime / 864000000000L) - 109207;


DateTime dt = DateTime.MinValue.AddDays(day).AddMilliseconds(ms);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM