0

我有以下有效的代码:

<%@ WebService Language="C#" Class="Absences" %>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Script;
using System.Web.Script.Services;
using System.Web.Services;

public class Absence
{
    public string name;
    public DateTime from;
    public DateTime to;

    public Absence(string m_name, DateTime m_from, DateTime m_to)
    {
        name = m_name;
        from = m_from;
        to = m_to;
    }

    public Absence() { }
}

[ScriptService]
public class Absences : WebService {
    List<Absence> Absence = new List<Absence>();

    SqlConnection connection;
    SqlCommand command;
    SqlDataReader reader;

    [WebMethod()]
    public List<Absence> GetAbsences(string strDateYear, string strDateMonth, string strDateDay)
    {
        var absences = new List<Absence>();

        using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
        {

            using (command = new SqlCommand(@"select full_name, from_date, to_date from table1", connection))
                {
                    connection.Open();
                    using (reader = command.ExecuteReader())
                    {
                        int NameIndex = reader.GetOrdinal("full_name");
                        int FromIndex = reader.GetOrdinal("from_date");
                        int ToIndex = reader.GetOrdinal("to_date");

                        while (reader.Read())
                        {
                            absences.Add(new Absence(reader.GetString(NameIndex), reader.GetDateTime(FromIndex), reader.GetDateTime(ToIndex)));
                        }
                    }
                }

        }

        return absences;
    }
}

唯一的问题是日期,它们在 JSON 数据中以一种奇怪的格式出现,例如:

"from":"\/Date(1353456000000)\/"

我怎样才能得到以下格式的日期dd/mm/yy

4

3 回答 3

1

这是解决方案

var jsondateString = "\/Date(1353456000000)\/".substr(6);
var current = new Date(parseInt(jsondateString ));
var month = current.getMonth() + 1;
var day = current.getDate();
var year = current.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);
于 2012-11-02T11:22:34.567 回答
0

这应该工作

var data = {"from":"\/Date(1353456000000)\/"};
var parsedDate = new Date(parseInt(data.from.substr(6)));
var curr_date = parsedDate.getDate();
var curr_month = parsedDate.getMonth() + 1;
var curr_year = parsedDate.getFullYear();
alert(curr_date + "/" + curr_month + "/" + curr_year);

工作小提琴 - http://jsfiddle.net/tariqulazam/a2xZM/

于 2012-11-02T11:15:55.797 回答
0

你可以使用moment.js。它是一个非常灵活的开源库。

一个 5kb 的 JavaScript 日期库,用于解析、验证、操作和格式化日期。

moment("/Date(1198908717056-0700)/").valueOf(); // 1198908717056
于 2012-11-02T11:22:16.473 回答