C# 将日期时间转换为 JSON 日期时间

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

Convert DateTime To JSON DateTime

c#.netjsonweb-services

提问by manoj

I have a WebService which return DateTime Field.

我有一个返回 DateTime 字段的 WebService。

I get a result /Date(1379048144000)/but

我得到了一个结果/Date(1379048144000)/但是

i want just 1379048144000how can i achieve that.

我想我1379048144000怎么能做到这一点。

[WebMethod]
public DateTime GetServerDate()
{
    return DateTime.Now;
}

by setting Header Content-Type: application/json; charset=utf-8; i got a result like /Date(1379048144000)/.

通过设置 Header Content-Type: application/json; 字符集=utf-8;我得到了类似的结果/Date(1379048144000)/

采纳答案by Joe

You could change your WS to return a long with the value of the DateTime. The value to return is the number of milliseconds since the Unix Epoch (01/01/1970). This could be done with an extension method on DateTimesomething like:

您可以更改您的 WS 以返回带有 DateTime 值的 long。要返回的值是自 Unix 纪元 (01/01/1970) 以来的毫秒数。这可以通过扩展方法来完成,DateTime例如:

public static class DateTimeExtensions
{
    ...
    private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);

    public static long ToUnixTime(this DateTime dateTime)
    {
        return (dateTime - UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond;
    }
    ...
}

And your web service method might look something like:

您的 Web 服务方法可能类似于:

public long GetMyDate(...)
{
    DateTime dateTime = ...;
    return dateTime.ToUnixTime();
}

回答by Zaheer Ahmed

try regex:

尝试正则表达式:

var jsonDate = @"/Date(1379048144000)/"; 
var regex = /-?\d+/; 
var jsonDate = re.exec(jsonDate); 
var dateOriginal = new Date(parseInt(m[0]));

回答by mrakodol

in client side you can use this function to show a right date to client(I use it on my projects):

在客户端,您可以使用此功能向客户端显示正确的日期(我在我的项目中使用它):

function parseJsonDate(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined) parts[2] = 0;
if (parts[3] == undefined) parts[3] = 0;
d = new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
date = d.getDate() + 1;
date = date < 10 ? "0" + date : date;
mon = d.getMonth() + 1;
mon = mon < 10 ? "0" + mon : mon;
year = d.getFullYear();
return (date + "." + mon + "." + year);
};

This function is return right date in format: dd.mm.yyyy, but you can change it if you need. I hope that I help you.

此函数以格式:dd.mm.yyyy 返回正确的日期,但您可以根据需要更改它。我希望我能帮助你。

回答by Matias

U can always solve your problem when sending a date in a JSON object to JS by converting the date as follows:

将 JSON 对象中的日期发送给 JS 时,您总是可以通过如下转换日期来解决您的问题:

var myJSDate = (new Date(parseInt(obj.MyDate.substr(6)))).toJSON();

Where obj.Date contains the date you wanna format.

其中 obj.Date 包含您要格式化的日期。

Then u'll get something like this: "2013-10-25T18:04:17.289Z"

然后你会得到这样的信息:“2013-10-25T18:04:17.289Z”

U can always check it in Chrome console by writing:

您可以随时在 Chrome 控制台中通过编写以下内容来检查它:

(new Date()).toJSON();

Hope this helps!

希望这可以帮助!

回答by Ben

with Json.NET:

使用Json.NET

string date = Newtonsoft.Json.JsonConvert.SerializeObject(DateTime.Now);    

回答by Karan Thakkar

Simply write like this to convert your date string in JSON format.

只需像这样编写即可将日期字符串转换为 JSON 格式。

date = "{" + date + "}";

回答by viorel

There are two solutions:

有两种解决方案:

  1. client side:
  1. 客户端:

function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

It is possible to need alsou to convert into data object var date = new Date(xxx)

也可能需要转换成数据对象 var date = new Date(xxx)

  1. Server side:

    Newtonsoft.Json.JsonConvert.SerializeObject(your_object)
    
  1. 服务器端:

    Newtonsoft.Json.JsonConvert.SerializeObject(your_object)