转换C#.NET DateTime.ticks在JavaScript天/小时/分钟小时、DateTime、NET、JavaScript

由网友(行走天空)分享简介:在我的系统,我存储持续时间蜱,它被传递给我的客户端的移动应用程序,并从那里我想蜱转换为人类可读的形式。就我而言,日,小时和分钟。 In my system, I am storing a duration in Ticks, which is being passed to my client mobile appl...

在我的系统,我存储持续时间蜱,它被传递给我的客户端的移动应用程序,并从那里我想蜱转换为人类可读的形式。就我而言,日,小时和分钟。

In my system, I am storing a duration in Ticks, which is being passed to my client mobile application, and from there I want to convert ticks into a human readable form. In my case, days, hours and minutes.

我的客户的移动应用程序是使用JavaScript codeD,所以这是我用什么时间转换为日/时/分。

My client mobile application is coded using Javascript, and so this is what I'm using to convert the duration to days/hours/minutes.

推荐答案

在C#.NET,单剔重presents百纳秒,或者千万分之一秒。 [来源] 。

In C# .NET, a single tick represents one hundred nanoseconds, or one ten-millionth of a second. [Source].

因此​​,为了计算由蜱的数目的天数(四舍五入至最接近的整数),予先计算由千万相乘,然后乘以产生的秒的数量的秒数天(60秒分钟,60分钟,小时,24小时天)。我用的是模运算符(%),以获得剩余价值构成的小时和分钟的时间。

Therefore, in order to calculate the number of days from the number of ticks (rounded to nearest whole numbers), I first calculate the number of seconds by multiplying by ten million, and then multiplying that by the number of seconds in a day (60 seconds in minute, 60 minutes in hour, 24 hours in day). I use the modulus operator (%) to get the remainder values that make up the duration of hours and minutes.

var time = 3669905128; // Time value in ticks
var days = Math.floor(time/(24*60*60*10000000)); // Math.floor() rounds a number downwards to the nearest whole integer, which in this case is the value representing the day
var hours = Math.round((time/(60*60*10000000)) % 24); // Math.round() rounds the number up or down
var mins = Math.round((time/(60*10000000)) % 60);

console.log('days: ' + days);   
console.log('hours: ' + hours);   
console.log('mins: ' + mins);

因此​​,在上述例子中,蜱的量为相当于6分钟(四舍五入)。

So, in the above example, the amount of ticks is equivalent to 6 minutes (rounded up).

和举另外一个例子,有2,193,385,800,000,000蜱,我们得到2538天,15个小时23分钟。

And to take another example, with 2,193,385,800,000,000 ticks, we get 2538 days, 15 hours and 23 minutes.

阅读全文

相关推荐

最新文章