如何在JavaScript中获取日期刻度? [英] How to get date ticks in a javaScript?
问题描述
我在C#中有这样的代码:
I have such a code in the C#:
public static decimal StringDateToDecimal(this string stringDate)
{
var cultureInfo = new CultureInfo("ru-Ru");
var date = DateTime.Parse(stringDate, cultureInfo);
var number = date.ToBinary();
var unix = new DateTime(1970, 1, 1);
var diff = (date - unix).Ticks;
return (decimal)diff;
}
对于2018年3月1日,它返回15198624000000000。
但是js中的代码:
For 01 march 2018 it returns 15198624000000000.
But code in the js:
var from = $input.split(".");
var d = new Date(from[2], from[1] - 1, from[0]);
var dStart = new Date(1970, 1, 1);
var seconds = d.getTime();
var secondsStart = dStart.getTime();
var dateDifference = seconds - secondsStart;
对于 01.03.2018,它返回1517184000000
For "01.03.2018" it returns 1517184000000
或
var ticks2 = ((d.getTime() * 10000) + 621355968000000000);
var ticks1 = ((dStart.getTime() * 10000) + 621355968000000000);
var difrent = ticks2 - ticks1;
我在js代码中做错了什么?
我想在js中获得相同的值
What am I doing wrong in the js code?
I want to get the same value in the js
推荐答案
-
JavaScript:
Date.getTime()
:一个数字,表示自1970年1月1日午夜以来的毫秒数。JavaScript:
Date.getTime()
: a number, representing the number of milliseconds since midnight January 1, 1970.C#:一个滴答声代表一百纳秒或一千万分之一秒。一毫秒有10,000个滴答声,一秒有1000万个滴答声。
C#: A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.
因此,
JavaScript滴答声 =( C#滴答声 / 10000)
,您的代码就可以了。您只需考虑选择的任何代码(C#或JavaScript)之间的差异。So,
JavaScript ticks = (C# ticks / 10000)
and your code looks OK. You just need to account for the difference in whichever code (C# or JavaScript) you choose.如果您使用JavaScript(明确的月月日期) (例如,2018年12月23日):
If you take (an unambiguous day-month date) in JavaScript (e.g. 23 December 2018):
var $input = "23.12.2018"; var from = $input.split("."); var dNew = new Date(from[2], from[1]-1, from[0]); //Note dStart has 0 as month because JavaScript dates start at 0 and end with 11 var dStart = new Date(1970, 0, 1); var seconds = dNew.getTime(); var secondsStart = dStart.getTime(); var dateDifference = seconds - secondsStart; // multiply by 10000 to reconcile to c# console.log("===> " + dateDifference * 10000);
如果使用
StringDateToDecimal( 2018年12月23日,两个答案都为15455232000000000 );
在您的C#代码中。Both answers come to 15455232000000000 if you use
StringDateToDecimal("23 december 2018");
in your C# code.我希望我能理解您的要求。
I hope I'm getting the jist of what you are asking.
这篇关于如何在JavaScript中获取日期刻度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!