发表日期网页API自定义格式 [英] Post dates web api custom format

查看:173
本文介绍了发表日期网页API自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一场噩梦(至少我无法找到一个简单的方法),我已搜查了几个小时,真的找不到我的解决方案。

This is a nightmare (at least I can’t find an easy way), I have searched for hours and really can’t find my solution.

你是如何处理在网页API自定义格式的日期?我需要DD / MM / YYYY格式

How do you handle dates in a custom format in web api? I need DD/MM/YYYY format

我不能发布我的日期,因为我想要的API他们在默认格式MM / DD / YY,一个解决办法是解析所有通过JavaScript我的API需要它的格式我的日期,但真的吗?这难道不是一种更清洁的方式?

I can’t post my dates since my api wants them to be in the default format MM/DD/YY, one solution would be to parse all my dates via JavaScript to the format my api needs it, but for real? Isn’t it a cleaner way?

我怎样才能让我的API知道我的格式?

How can I make my api knows my format?

我发现的帖子,它说,它不适合邮寄工作。并没有解决呢。

I found this post, and it says it does not work for posting. And no solution yet.

我在错误的方式?太多了票的问题,甚至没有一个很好的答案!

Am I in the wrong way? Too many up votes for the question, and not even a good answer!

推荐答案

默认情况下,为JSON格式,在We​​b.Api,使用Json.NET串行器。 Json.NET在 ISO 8601 格式写入日期。在UTC日期都写有Z后缀。在当地时间的日期包括时区偏移量。例如:

By default, as JSON formatter, in Web.Api, used Json.NET serializer. Json.NET writes dates in ISO 8601 format. Dates in UTC are written with a "Z" suffix. Dates in local time include a time-zone offset. For example:

2012-07-27T18:51:45.53403Z         // UTC
2012-07-27T11:51:45.53403-07:00    // Local

您可以覆盖 serrializer通过设置网站的DateFormatString财产的日期和时间设置API配置部分:

You can override serrializer date time settings by setting the DateFormatString property in web api configuration section:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd";
    }
}

此外,可能的是,你需要在Global.asax中更改线程的区域性设置

Also, probably, you will need to change thread culture settings in Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
{            
    var newCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
    newCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
    newCulture.DateTimeFormat.ShortTimePattern = "HH:mm";
    newCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
    newCulture.DateTimeFormat.DateSeparator = "-";
    Thread.CurrentThread.CurrentCulture = newCulture;
}

这篇关于发表日期网页API自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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