How to convert JavaScript date object to ticks

后端 未结 6 1780
[愿得一人]
[愿得一人] 2020-11-28 04:54

How should I convert JavaScript date object to ticks? I want to use the ticks to get the exact date for my C# application after synchronization of data.

6条回答
  •  孤城傲影
    2020-11-28 05:05

    The JavaScript Date type's origin is the Unix epoch: midnight on 1 January 1970.

    The .NET DateTime type's origin is midnight on 1 January 0001.

    You can translate a JavaScript Date object to .NET ticks as follows:

    var yourDate = new Date();  // for example
    
    // the number of .net ticks at the unix epoch
    var epochTicks = 621355968000000000;
    
    // there are 10000 .net ticks per millisecond
    var ticksPerMillisecond = 10000;
    
    // calculate the total number of .net ticks for your date
    var yourTicks = epochTicks + (yourDate.getTime() * ticksPerMillisecond);
    

提交回复
热议问题