将 C# 日期时间解析为 javascript 日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15829521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:15:17  来源:igfitidea点击:

Parsing the C# datetime to javascript datetime

c#javascriptjquerydatetime

提问by Snake Eyes

I know that my question is similar to others but I didn't found any solution to my problem.

我知道我的问题与其他人类似,但我没有找到任何解决我问题的方法。

I have a C# DateTime property

我有一个 C# DateTime 属性

 public DateTime MyDate { get;set;}

When I use an ajax to get some information, I wrote in javascript something like:

当我使用 ajax 获取一些信息时,我在 javascript 中写了如下内容:

$.each(object, function(k,v){
  alert(object.MyDate);
});

It returns something like:

它返回如下内容:

/Date(1362478277517)/

It is possible to convert that datetime to javascript date ?

可以将该日期时间转换为 javascript 日期吗?

Thank you.

谢谢你。

回答by Rob

new Date(object.MyDate);should work.

new Date(object.MyDate);应该管用。

EDIT: var date = new Date(parseInt(object.MyDate.substr(6)));

编辑: var date = new Date(parseInt(object.MyDate.substr(6)));

I've also seen this method:

我也见过这个方法:

var milli = "/Date(1245398693390)/".replace(/\/Date\((-?\d+)\)\//, '');
var d = new Date(parseInt(milli));

回答by Henry Rodriguez

I'm using .Net Core 2.0. & MySQL 5.7

我正在使用 .Net Core 2.0。和 MySQL 5.7

In my current development, I'm assigning the returned value directly into the DOM object like this:

在我目前的开发中,我将返回值直接分配给 DOM 对象,如下所示:

DOMControl.value = response.CreatedOn.toString().split(".")[0];

I'm returning a JsonResultof the resulting object, the resulting JSON arrives with the date value as follows:

我正在返回结果对象的JsonResult,结果 JSON 与日期值一起到达,如下所示:

{
  ...
  createdOn : "2017-11-28T00:43:29.0472483Z"
  ...
}

I hope this help to somebody.

我希望这对某人有帮助。