修剪JSON对象值

问题描述 投票:0回答:1

我有一个表单,通过点击按钮从API获取JSON数据,并获取值并将其显示为asp:label的文本

我得到的JSON数据:

[{"Name":"Tom",date":"2018-02-24T01:52:56.3229211Z"}]

我的代码如:Body(只是重要部分):

<body>
    <h2 class="auto-style3">SPACCO WEB</h2>
    <form id="form1" runat="server" class="auto-style1">
    <asp:Label ID="info1"    runat="server"  Text="Name:"></asp:Label>
    <asp:Label ID="info1Val" runat="server"  Text=""></asp:Label>
    <br />
    <asp:Label ID="info2"    runat="server" Text="Date:"></asp:Label>
    <asp:Label ID="info2Val" runat="server" Text=""></asp:Label>
    <br />
</body>

这里的重要性在于我将数据作为text=""放在ID=info2val

剧本:

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
 $('#search').click(function () {
            $.ajax({

                url: "http://192.168.5.37/api/find/" + $("input#manualuser").val(),
                type: "GET",
                dataType: 'Jsonp',
                success: function (result) {
                    $('#<%=info2Val.ClientID%>').html(result[0].Name);
                    $('#<%=info3Val.ClientID%>').html(result[0].Data);
                }
            });
        })

结果:

Name: 
Tom

Date:
2018-02-24T01:52:56.3229211Z

问题:一切都按预期工作但我的问题是我想将日期值修剪为:2018-02-24(并从T 01:52:56.3229211Z切割所有内容)

请帮助我该怎么做。

javascript asp.net json
1个回答
0
投票

在日期中找到T的索引,然后使用子字符串。

var date = // Put your date here
var index = date.indexOf('T')
if (index != -1) {
  var shortened = date.substring(0, index)
}
© www.soinside.com 2019 - 2024. All rights reserved.