Boy*_*smo 3 .net javascript c# nodatime momentjs
我正在使用 nodatime 并返回刻度。如何使用 momentjs 将刻度转换为使用和格式?
public JsonResult Foo()
{
var now = SystemClock.Instance.Now.Ticks;
return Json(now, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
它返回long如14598788048897648.
Moment.js 没有直接接受刻度的构造函数,但是它确实有一个接受自 epoch 以来经过的毫秒数的构造函数,这可能适用于此:
// Dividing your ticks by 10000 will yield the number of milliseconds
// as there are 10000 ticks in a millisecond
var now = moment(ticks / 10000);
Run Code Online (Sandbox Code Playgroud)
NodaTime 存储库中的此 GitHub 讨论讨论了使用扩展方法来支持此行为以及从服务器端代码返回毫秒数:
public static long ToUnixTimeMilliseconds(this Instant instant)
{
return instant.Ticks / NodaConstants.TicksPerMillisecond;
}
Run Code Online (Sandbox Code Playgroud)