将C#.NET DateTime.ticks转换为日/小时/分钟的JavaScript [英] Convert C# .NET DateTime.ticks to days/hours/mins in JavaScript

查看:929
本文介绍了将C#.NET DateTime.ticks转换为日/小时/分钟的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的系统中,我在Ticks中存储一个持续时间,该持续时间正在传递给我的客户端移动应用程序,从那里我想将刻录转换成人类可读的形式。在我的情况下,天,小时和分钟。



我的客户端移动应用程序使用Javascript进行编码,所以这正是我用来将持续时间转换为天/小时/分钟。

解决方案

在C#.NET中,单个刻度代表一百纳秒,或百万分之一秒。 [来源]



因此,为了计算从滴答数(四舍五入到最接近的整数)的天数,我首先计算秒数乘以千万,然后将其乘以一天中的秒数(60分钟,60分钟,小时,24小时)。我使用模数运算符(%)来获得构成小时和分钟持续时间的余数值。

  var time = 3669905128; //时间值在ticks 
var days = Math.floor(time /(24 * 60 * 60 * 10000000)); // Math.floor()将一个数字向下舍入到最接近的整数,在这种情况下,该值代表一天
var hours = Math.round((time /(60 * 60 * 10000000))% 24); // Math.round()向上或向下舍入
var mins = Math.round((time /(60 * 10000000))%60);

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

所以,在上面的例子中,刻度数相当于6分钟(向上舍入)。另外还有另外一个例子,有2,193,385,800,000点钟,我们得到2538天,15小时23分钟。


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.

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

解决方案

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

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);

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

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

这篇关于将C#.NET DateTime.ticks转换为日/小时/分钟的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆