How can I convert ticks to a date format?

前端 未结 4 511
心在旅途
心在旅途 2020-11-27 20:06

I am converting a ticks value to a date like this:

Convert(datetime, (MachineGroups.TimeAdded - 599266080000000000)/864000000000);

Using th

4条回答
  •  無奈伤痛
    2020-11-27 20:25

    Answers so far helped me come up with mine. I'm wary of UTC vs local time; ticks should always be UTC IMO.

    public class Time
    {
        public static void Timestamps()
        {
            OutputTimestamp();
            Thread.Sleep(1000);
            OutputTimestamp();
        }
    
        private static void OutputTimestamp()
        {
            var timestamp = DateTime.UtcNow.Ticks;
            var localTicks = DateTime.Now.Ticks;
            var localTime = new DateTime(timestamp, DateTimeKind.Utc).ToLocalTime();
            Console.Out.WriteLine("Timestamp = {0}.  Local ticks = {1}.  Local time = {2}.", timestamp, localTicks, localTime);
        }
    }
    

    Output:

    Timestamp = 636988286338754530.  Local ticks = 636988034338754530.  Local time = 2019-07-15 4:03:53 PM.
    Timestamp = 636988286348878736.  Local ticks = 636988034348878736.  Local time = 2019-07-15 4:03:54 PM.
    

提交回复
热议问题