I'm developing an application which deals with date and time in frontend, with Javascript's Date object, and sends this information to the API based on C#... So far I'm thinking about converting the date and time selected by the user in the frontend using the getTime() function, so I can get its milliseconds and then convert to DateTime in C# with the following function
public static DateTime GetDateFromMilliseconds(double unixTimeStamp)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
So, I'd like to know, is there another better way to deal with it?
Newer versions of C# include methods for working with Unix timestamps.
For this case, using DateTimeOffset.FromUnixTimeMilliseconds() will save you a few lines of code.
Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.fromunixtimemilliseconds?view=net-5.0