如何在Javascript或jquery中将C#DateTime.Ticks转换为javascript日期? [英] How do I convert C# DateTime.Ticks to javascript date in javascript or jquery?

查看:102
本文介绍了如何在Javascript或jquery中将C#DateTime.Ticks转换为javascript日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,

所以我得到以下值(实际上是C#DateTime.Ticks)635556672000000000到javascript中的Date对象?



我一直在谷歌搜索相当多,但无法找到帮助我的答案。



所以我在这里试试运气: )



谢谢!

Hey there,
so I am getting the following value ( which is indeed C# DateTime.Ticks ) 635556672000000000 to a Date object in javascript?

I've been googling for quite a bit but wasn't able to find an answer that helps me.

So I am trying my luck here :)

Thank you!

推荐答案

你需要ASP.NET WebService的帮助/可以在当前时间传回JavaScript的MVC / Web表单等;通过页面代码本身,或通过AJAX请求。



这里有一些C#代码将ticks整数转换为microtime时间戳:





You'll need the help of an ASP.NET WebService / MVC / Web Form or the like that can pass back to your JavaScript the current time; either through page code itself, or through an AJAX request.

Here's some C# code that translates a ticks integer to a microtime timestamp:


DateTime someDate = new DateTime(635556672000000000); // or whatever variable has your ticks
Int32 unixTimestamp = (Int32)(someDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
unixTimestamp = unixTimestamp * 1000;





一旦你'设置了一些内容,以便您可以从ASP.NET资源中获取unixTimestamp的值,只需通过jQuery等调用它。例如,假设您创建一个名为time.ashx的ASP.NET Web处理程序,它将缩略图标记作为明文吐出:





Once you've set something up so that you can get the value of unixTimestamp out of an ASP.NET resource, just call it via jQuery or the like. For example, supposing you create an ASP.NET web handler, named time.ashx, that spits out the microtime stamp as plaintext:

var myTimestamp = new Date(); //set default date to now


.get(< span class =code-string> http://www.example.com/time.ashx function (){})。done( function (data){
/ / 更新日期及来自get request的数据
myTimestamp = new 日期(数据);
});
.get("http://www.example.com/time.ashx", function() { }).done(function(data) { //update date with data from get request myTimestamp = new Date(data); });





或类似的东西。要点是:ASP.NET助手将ticks转换为microtime。



Or something similar. Point is: ASP.NET helper to convert ticks to microtime.


在考虑了这一点之后,事实证明你可以用数学方法将ticks转换为仅在JavaScript中的microtime。



After thinking about this some, it turns out you can do math to convert ticks to microtime in JavaScript alone.

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 = 2208988800000;

//new date is ticks, converted to microtime, minus difference from epoch microtime
var tickDate = new Date(ticksToMicrotime - epochMicrotimeDiff);





tickDate现在是您的滴答时间戳反映的日期。



http:/ /msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx [ ^ ]



另外,protip:尝试更愉快,你会发现人们更愿意帮助你。我是一个人,没有计划再次帮助你。



tickDate is now the date reflected by your ticks timestamp.

http://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx[^]

Also, protip: Try to be more pleasant, you'll find people are more willing to help you. I, for one, have no plans to assist you again.


这篇关于如何在Javascript或jquery中将C#DateTime.Ticks转换为javascript日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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