【问题标题】:How to display date time from c# in javascript?如何在javascript中显示来自c#的日期时间?
【发布时间】:2013-11-21 16:36:36
【问题描述】:

我有课

http://pastebin.com/z5JVWCms

 public string Tankerler()
    {
        DataTable dt = new DataTable();

        using (SqlConnection con = new SqlConnection("Data Source=local;Initial Catalog=DB;User ID=se;Password=1"))
        {
            using (

                SqlCommand cmd = new SqlCommand("select OrderDate=Siparisler.DateScheduled,uruncikistarih=UrunTransfer.DateRealized,Siparis_Veren_Firma=Bayi.FirmName,plate=UrunTransfer.Plate,Driver=Surucu.Name,lat=TankerKonum.Lat,lng=TankerKonum.Lng,Speed=TankerKonum.Speed,Zaman=TankerKonum.ReadTime,IrsaliyeNo=UrunTransfer.PrintOutID from ProductTransfer as UrunTransfer join TransferOrder as Siparisler on Siparisler.OID=UrunTransfer.TransferOrderID Join Dealer as Bayi on Bayi.OID=Siparisler.DealerID Join Driver as Surucu on Surucu.OID=Siparisler.DriverID join devcocom_admin.TankerLocation as TankerKonum on  TankerKonum.TankerID=Siparisler.TankerID where UrunTransfer.DateRealized >DATEADD(HOUR, - 24, GETDATE()) and TankerKonum.OID in (Select MAX(TankerKonum.OID) from  devcocom_admin.TankerLocation as TankerKonum group by TankerKonum.TankerID)", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return serializer.Serialize(rows);
            }
        }

    }

我通过这个类从数据库中获取了一些字段。

我的javascript代码

http://pastebin.com/EpPb9Yr3

    var markers2 = JSON.parse('<%=Tankerler() %>');


    for (i = 0; i < markers2.length; i++) {

    var data2 = markers2[i]

    title: "Plate:" + " " + " " + data2.plate+ "\n" + "Speed:" +

    " " + data2.speed+ " " + "h/km" + "\n" + "Driver: " + " " + data2.Drvier+ "\n" + 

    "Print OUT ID :" + " " + data2.prntID+" "+"OrderDate: "+data2.ordDate,

}

结果是这样的:

车牌:34AAA34 速度:120 h/km 司机:John Print OUT ID:ABC12345 OrderDate: /Date(138472560000)/

但在 sql OrderDate 中:2013-11-20 21:24:10.000

【问题讨论】:

  • 请在此处粘贴您的代码(并正确格式化!)不要让我们徘徊到另一边。
  • 我会假设数据库中的列不是字符串,而是某种日期/时间类型。这意味着您提供的 OrderDate 是转换为格式化字符串 的值。它不是以这种方式存储的。我对Date Format Problem的回答中有更多细节。

标签: c# javascript datatable


【解决方案1】:

您正在使用JavaScriptSerializer,这就是它序列化日期的方式。 如果您想将它们序列化为不同的格式,您可以尝试使用其他序列化器,例如Json.NET

来自他们的网站:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Sizes": [
//    "Small"
//  ]
//}

【讨论】:

  • +1 for Json.NET - 无论如何,JavaScript new Date(138472560000) 将评估为特定的 Date 对象(或者,更好的是,使用 moment.js 以便它也可以很好地格式化)。跨度>
【解决方案2】:

不幸的是,一些 JSON 序列化程序选择了这种日期格式。而且我会建议切换到发送正确的 ISO8601 格式日期的日期(我还建议使用 DateTimeOffset 以便 TZ 永远不会模棱两可!),无论如何..


这个想法是data2.ordDate,它的字符串值为“/Date(138472560000)/”,代表new Date(138472560000) - 或者,毫秒数Unix epoch

// where dt is in that silly format, extract the "Unix epoch" in milliseconds
function getEpoch (dt) {
  return parseInt(dt.match(/\d+/)[0], 10);
}

可以这样使用:

var ordDate = new Date(getEpoch(data2.ordDate))

然后formatting with moment.js 超级简单(但不是必需的):

var str = moment(ordDate).format("YYYY-MM-dd HH:mm:ss")

【讨论】:

  • var ordDate= data2.ordDate; ordDate= ordDate.replace(/[^0-9 +]/g, ''); var myDate = new Date(parseInt(ordDate)).toString("dd-MM-yyyy");
猜你喜欢
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 2019-06-01
  • 2011-05-21
  • 1970-01-01
  • 2017-10-18
相关资源
最近更新 更多