C# 日期以奇怪的格式出现
我有以下有效的代码:C# 日期以奇怪的格式出现,c#,asp.net,json,.net-3.5,C#,Asp.net,Json,.net 3.5,我有以下有效的代码: <%@ 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.We
<%@ 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;
}
}
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用系统配置;
使用系统数据;
使用System.Data.SqlClient;
使用System.Web;
使用System.Web.Script;
使用System.Web.Script.Services;
使用System.Web.Services;
公共课缺席
{
公共字符串名称;
公共日期时间从;
公开日期和时间;
公共缺勤(字符串m_name、日期时间m_from、日期时间m_to)
{
name=m_name;
from=m_from;
to=m_to;
}
公共缺席(){}
}
[脚本服务]
公共类缺席:WebService{
列表缺失=新列表();
SqlConnection连接;
SqlCommand命令;
SqlDataReader;
[WebMethod()]
公共列表GetAbscences(字符串strDateYear、字符串strDateMonth、字符串strDateDay)
{
var缺席=新列表();
使用(connection=newsqlconnection(ConfigurationManager.AppSettings[“connString”]))
{
使用(command=newsqlcommand(@“选择完整名称,从表1中的日期,到表1中的日期”,连接))
{
connection.Open();
使用(reader=command.ExecuteReader())
{
int NameIndex=reader.GetOrdinal(“全名”);
int FromIndex=reader.GetOrdinal(“起始日期”);
int-ToIndex=reader.GetOrdinal(“截止日期”);
while(reader.Read())
{
添加(新缺勤(reader.GetString(NameIndex)、reader.GetDateTime(FromIndex)、reader.GetDateTime(ToIndex));
}
}
}
}
返回缺席;
}
}
唯一的问题是日期,它们在JSON数据中以奇怪的格式出现,例如:
“from”:“\/日期(1353456000000)\/”
如何将日期转换为以下格式dd/mm/yy
?这应该有效
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);
工作小提琴-您可以使用它。它是一个非常灵活的开源库
一个5kb的javascript日期库,用于解析、验证、操作和格式化日期
这是解决办法
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);
这与jQuery有什么关系?这将对您有所帮助。(数据来源于substr(6));不会像日期(1353456000000)一样工作,更不用说\/是否也会seen@PaulSullivan-看小提琴。当你做parseInt时,最后所有非数字的东西都被去掉了。。。这真是太神奇了!(还有一些人在parseInt中过度热衷于编码。。。
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);