admin管理员组文章数量:1279124
I'm using nodatime and it returns ticks. How can I convert ticks to use and format using momentjs?
public JsonResult Foo()
{
var now = SystemClock.Instance.Now.Ticks;
return Json(now, JsonRequestBehavior.AllowGet);
}
it returns long
such as 14598788048897648
.
I'm using nodatime and it returns ticks. How can I convert ticks to use and format using momentjs?
public JsonResult Foo()
{
var now = SystemClock.Instance.Now.Ticks;
return Json(now, JsonRequestBehavior.AllowGet);
}
it returns long
such as 14598788048897648
.
3 Answers
Reset to default 5Moment.js doesn't have a constructor that directly accepts ticks, however it does have one that accepts the number of milliseconds that have elapsed since the epoch, which might be suitable for this :
// Dividing your ticks by 10000 will yield the number of milliseconds
// as there are 10000 ticks in a millisecond
var now = moment(ticks / 10000);
This GitHub discussion in the NodaTime repository discusses the use of an extension method to support this behavior as well to return the number of milliseconds from your server-side code :
public static long ToUnixTimeMilliseconds(this Instant instant)
{
return instant.Ticks / NodaConstants.TicksPerMillisecond;
}
Don't leak Ticks
out of your API. Instead, use the NodaTime.Serialization.JsonNet
package to allow NodaTime types like Instant
to be serialized in ISO8601 standard format. That format is supported natively in moment.js.
See the user guide page on serialization, towards the bottom of the page.
According to the documentation, the Instant.Ticks property is:
The number of ticks since the Unix epoch.
And,
A tick is equal to 100 nanoseconds. There are 10,000 ticks in a millisecond.
A Date object takes the number of milliseconds since the Unix epoch in its constructor, and since moment uses the Date constructor underneath the covers, you can just use:
var value = moment(ticks/10000);
本文标签: javascripthow to convert ticks to momentjs objectStack Overflow
版权声明:本文标题:javascript - how to convert ticks to momentjs object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741263479a2368040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论