在 JavaScript 中将 C# .NET DateTime.ticks 转换为天/小时/分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15486299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert C# .NET DateTime.ticks to days/hours/mins in JavaScript
提问by Ciaran Gallagher
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.
在我的系统中,我在 Ticks 中存储了一个持续时间,它被传递到我的客户端移动应用程序,然后我想从那里将 ticks 转换为人类可读的形式。就我而言,天、小时和分钟。
My client mobile application is coded using Javascript, and so this is what I'm using to convert the duration to days/hours/minutes.
我的客户端移动应用程序是使用 Javascript 编码的,所以这就是我用来将持续时间转换为天/小时/分钟的方法。
采纳答案by Ciaran Gallagher
In C# .NET, a single tick represents one hundred nanoseconds, or one ten-millionth of a second. [Source].
在 C# .NET 中,一个滴答声代表一百纳秒,或百万分之一秒。[来源].
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.
因此,为了从刻度数(四舍五入到最接近的整数)计算天数,我首先通过乘以千万计算秒数,然后再乘以一天中的秒数(60分秒,小时 60 分钟,一天 24 小时)。我使用模数运算符 (%) 来获取构成小时和分钟持续时间的余数。
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).
因此,在上面的示例中,滴答数等于 6 分钟(向上取整)。
And to take another example, with 2,193,385,800,000,000 ticks, we get 2538 days, 15 hours and 23 minutes.
再举一个例子,有 2,193,385,800,000,000 个滴答声,我们得到 2538 天 15 小时 23 分钟。
回答by pkmelee337
var ticks = 635556672000000000;
//ticks are in nanotime; convert to microtime
var ticksToMicrotime = ticks / 10000;
//ticks are recorded from 1/1/1; get microtime difference from 1/1/1/ to 1/1/1970
var epochMicrotimeDiff = Math.abs(new Date(0, 0, 1).setFullYear(1));
//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);
According to this pagethe setFullYear method returns "A Number, representing the number of milliseconds between the date object and midnight January 1 1970".
根据此页面,setFullYear 方法返回“一个数字,表示日期对象和 1970 年 1 月 1 日午夜之间的毫秒数”。
Check out this pagefor all the methods from the javascript Date object.
查看此页面以了解来自 javascript Date 对象的所有方法。
回答by citykid
You need to consider 2 things:
你需要考虑两件事:
Resolution
Ticks in .Net's DateTime are 0.1 Microsecond, while Javascript counts Milliseconds.
.Net 的 DateTime 中的分辨率刻度是 0.1 微秒,而 Javascript 计数毫秒。
Offset
In addition, .Net counts from 1.1.0000 while Javascript counts from 1.1.1970.
Offset
此外,.Net 从 1.1.0000 开始计数,而 Javascript 从 1.1.1970 开始计数。
TeaFiles.Nethas a Time class that uses Java = Javascript ticks. It has a scale property and a predefined Timescale.Java scale, that converts from .Net to Javascript.
TeaFiles.Net有一个使用 Java = Javascript 刻度的 Time 类。它有一个 scale 属性和一个预定义的 Timescale.Java 比例,可以从 .Net 转换为 Javascript。
回答by tecla
At the server-side, you can use a extension method, like this:
在服务器端,您可以使用扩展方法,如下所示:
public static class DateTimeExtensions {
private static readonly long UnixEpochTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
public static long? ToJavascriptTicks(this DateTime? value) {
return value == null ? (long?)null : (value.Value.ToUniversalTime().Ticks - UnixEpochTicks) / 10000;
}
public static long ToJavascriptTicks(this DateTime value) {
return (value.ToUniversalTime().Ticks - UnixEpochTicks) / 10000;
}
}
With this extensions, you can get the javascript ticks, and then you simply pass them to the client-side.
使用此扩展,您可以获得 javascript 滴答,然后您只需将它们传递给客户端。
If you are using MVC:
如果您使用的是 MVC:
You have the ViewModel:
你有视图模型:
public class MyViewModel {
public long MyJsTicks { get; set; }
}
And the Controller:
和控制器:
public ActionResult Action() {
long myJsTicks = DateTime.UtcNow.ToJavascriptTicks(); //<-- use the extension method
MyViewModel viewModel = new MyViewModel();
viewModel.MyJsTicks = myJsTicks;
return View(viewModel);
}
At the client-side:
在客户端:
var jsticks = <the js ticks obtained from server-side>;
var mydatetime = new Date(jsticks);
If you are using Razor view engine for your mobile app, getting the calculated js ticks from the server-side in your view is extremely simple, using a in-line expression:
如果您在移动应用程序中使用 Razor 视图引擎,那么使用内联表达式从服务器端获取计算的 js 刻度非常简单:
var jsticks = @(Model.MyJsTicks);
var mydatetime = new Date(jsticks);
Finally, to get days, hours and minutes from the javascript Date object:
最后,从 javascript Date 对象中获取天数、小时数和分钟数:
var hours = mydatetime.getHours();
var minutes = mydatetime.getMinutes();
var seconds = mydatetime.getSeconds();
(as you can see in the javascript "Date" object reference: https://www.w3schools.com/jsref/jsref_obj_date.asp)
(正如您在 javascript“日期”对象参考中所见:https: //www.w3schools.com/jsref/jsref_obj_date.asp)