0

I have a class

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);
            }
        }

    }

I get some field from database by this class.

My javascript code

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,

}

Result is like this :

Plate :34AAA34 Speed:120 h/km Driver:John Print OUT ID:ABC12345 OrderDate: /Date(138472560000)/

But in sql OrderDate : 2013-11-20 21:24:10.000

2
  • 1
    Please paste your code here (and properly format!) Don't make us wander off to another side. Commented Nov 21, 2013 at 8:17
  • I will assume that the column in the database is not a string but some sort of date/time type. This means that the OrderDate that you present is the value converted into a formatted string. It is not stored this way. There are more details in my answer to Date Format Problem. Commented Nov 21, 2013 at 8:25

2 Answers 2

3

You are using the JavaScriptSerializer, and this is how it serializes dates. If you want to serialize them to a different format, you can try another serializer such as Json.NET.

From their website:

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"
//  ]
//}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for Json.NET - anyway, the JavaScript new Date(138472560000) will evaluate to the specific Date object (or, better, using moment.js so that it can also be formatted nicely).
2

Unfortunately, some JSON serializers choose this date format. And I would recommend switching to one that sends proper ISO8601-formatted dates (and I would also recommend using DateTimeOffset such that the TZ is never ambiguous!), anyway ..


The idea is that data2.ordDate, which has a string value of "/Date(138472560000)/", represents new Date(138472560000) - or, the number of milliseconds sinch 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);
}

Which can be used like so:

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

And then formatting with moment.js is super easy (but it's not required):

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

1 Comment

var ordDate= data2.ordDate; ordDate= ordDate.replace(/[^0-9 +]/g, ''); var myDate = new Date(parseInt(ordDate)).toString("dd-MM-yyyy");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.