我有以下代码可以使用:
<%@ 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
?
答案 0 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分:0)
您可以使用moment.js。它是一个非常灵活的开源库。
一个5kb的javascript日期库,用于解析,验证,操作和格式化日期。
moment("/Date(1198908717056-0700)/").valueOf(); // 1198908717056