【问题标题】:Issue with asmx webservice Date objectasmx webservice Date 对象的问题
【发布时间】:2013-07-08 07:33:24
【问题描述】:

我正在使用java scriptDate 对象解析为Web 服务,如下所示。

HRA_Create.HelloWorld(new Date("2013-07-08 00:00:00"));

但是当我在asmx WebMethod 中检查这个Date 时,显示为7/7/2013 6:00:00 PM

我调试我的应用程序并看到如下值。

Debug.WriteLine(dt.ToString());

可能是什么错误?我该如何解决这个问题?

    [WebMethod]
    public string HelloWorld(DateTime dt)
    {
        Debug.WriteLine(dt.ToString());

        return dt.ToString();
    }

Java 脚本

<script type="text/javascript">
    function callServer() {
        HRA_Create.HelloWorld(new Date("2013-07-08 00:00:00"));
    }
</script>

脚本管理器

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/HRA_Create.asmx" />
    </Services>
</asp:ScriptManager>

Java 脚本调用

<a href="javascript:callServer()">Call Server</a>

【问题讨论】:

  • 两个日期不同,您可能需要检查您的代码,这不是格式问题。
  • @Nil 抱歉。我的复制粘贴问题。我已经更新了我的帖子。返回值为7/7/2013 6:00:00 PM
  • 2013-07-08 和 7/7/2013 不同,对吗?
  • @Nil 不,这是不正确的。那就是问题所在。我将2013-07-08 传递给WebMethod,它返回7/7/2013。这是不正确的。我需要从WebMethod 返回传递的值。
  • 发布您的网络方法代码。

标签: javascript asp.net web-services asmx


【解决方案1】:

您需要使用以下符合 ISO 8601 的W3C formats 之一:

Year:
   YYYY (eg 1997)
Year and month:
   YYYY-MM (eg 1997-07)
Complete date:
   YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
   YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
   YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
   YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

where:

YYYY = four-digit year
MM   = two-digit month (01=January, etc.)
DD   = two-digit day of month (01 through 31)
hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
mm   = two digits of minute (00 through 59)
ss   = two digits of second (00 through 59)
s    = one or more digits representing a decimal fraction of a second
TZD  = time zone designator (Z or +hh:mm or -hh:mm)

在您的示例中,以下任何一项都应发送正确的日期:

HRA_Create.HelloWorld(new Date("2013-07-08T00:00:00Z"));
HRA_Create.HelloWorld(new Date("2013-07-08"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多