Reputation: 2976
In SQL Server:
DECLARE @UTC BIGINT
SET @UTC = 1563527339733
SELECT DATEADD(MILLISECOND, @UTC % 1000, DATEADD(SECOND, @UTC / 1000, '19700101'))
SELECT CONVERT(VARCHAR(8),cast('2019-07-19 09:08:59.733' as datetime), 108) 'hh:mi:ss'
Result: 09:08:59
But in C# or ASP.Net MVC:
DateTime dtCheck = DateTimeHelper.ConvertMilisecondToDateTimeUTC(1563527339733);
string test = dtCheck.ToString("HH:mm:ss");
Result of test in C#: 09:09:00
Function Convert
public static DateTime ConvertMilisecondToDateTimeUTC(long milisecond)
{
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(milisecond / 1000d));
return dt;
}
How can change it to the same?
Upvotes: 1
Views: 93
Reputation: 13237
Removing the Math.Round()
in the ConvertMilisecondToDateTimeUTC()
function, will return your expected result.
public static DateTime ConvertMilisecondToDateTimeUTC(long milisecond)
{
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(milisecond / 1000d);
return dt;
}
Please find the working demo
Upvotes: 1