Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys I have a problem I used this line:

C#
DateTime DateUTCKin1 = (new DateTime()).AddMilliseconds(TimeKin1);


To convert my "TimeKin1" which is in milisecond and is equal to 1664459293023, it is suposed to be 29 sept 2022 13:48 but the result of this line is 29 sept 0053 13:48. So it is not the right year and i don't know why. Do you ? Thank you for your answers

What I have tried:

My "TimeKin1" is in long like the methods asks. I've tried others method to convert my milisecond into UTC but this one seems to me the easier and more logical one.
Posted
Updated 26-Oct-22 22:16pm

Your "milliseconds" value is obviously relative to a base date/time.

By subtracting the milliseconds from the known value it is supposed to represent, we can find that base value:
C#
DateTime baseDate = new DateTime(2022, 9, 29, 13, 48, 0, 0, DateTimeKind.Utc).AddMilliseconds(-1664459293023);
// 31/12/1969 23:59:46
That seems like an odd choice, so I'm assuming you've simply missed out 13 seconds from your expected value. That would leave you with a base date of 1970-01-01T00:00:00Z, which is the Unix epoch[^].

You will need to use that value in your calculations:
C#
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static DateTime ConvertMillisecondsToTime(long milliseconds)
{
    return UnixEpoch.AddMilliseconds(milliseconds);
}
That will give you:
C#
DateTime DateUTCKin1 = ConvertMillisecondsToTime(TimeKin1);
// 2022-09-29T13:48:13Z

NB: If the milliseconds actually represent the "Unix time", then you could use the DateTimeOffset.FromUnixTimeMilliseconds[^] method instead, which would give you a DateTimeOffset value:
C#
DateTimeOffset DateUTCKin1 = DateTimeOffset.FromUnixTimeMilliseconds(TimeKin1);
// 2022-09-29T13:48:13Z
 
Share this answer
 
v3
Comments
OriginalGriff 27-Oct-22 4:03am    
:thumbsup:
Member 15802708 27-Oct-22 4:09am    
AH yhea you're right I used an online convert to see if the time was right and it was a unix epoch converter so it gives me the right time, thank you very much !
Your value is incorrect, 29/09/2022 13:48 is 62135596786977 in milliseconds.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900