0

A function in my web application makes a call to custom API (written in C#) that stores, among many things, current date & time to the database and in the API, a value is assigned simply using the DateTime.Now method.

And the above mentioned date & time value is retrieved in the following fashion:

DateTime? dt = !string.IsNullOrEmpty((dr["RecordDate"]).ToString()) ? 
    Convert.ToDateTime(dr["RecordDate"].ToString()) : (DateTime?) null;

When I assign the value in dt from above code to a JavaScript variable, I end up with a value /Date(1415049596057)/. I've seen date & time values being formatted in different ways before but I don't think I've seen anything quite like this. I suppose I'm not handling the conversion process correctly but I'm really puzzled by this.

UPDATE:

Value assignment to the DateTime variable is done as follows:

using (var con = new SqlConnection(Service.GetSiteSetting(_httpContextAccessor.Current(), customdb))
{
    con.Open();
    var cm = new SqlCommand { CommandType = CommandType.StoredProcedure, CommandText = "SaveRecord", Connection = con };
    ...  // not showing other parameters
    cm.Parameter.Add(new SqlParameter("@RecordDate", SqlDbType.DateTime) { Value = DateTime.Now });
    ...
    cm.ExecuteNonQuery();
}
Lucas Fowler
  • 189
  • 2
  • 12
BinaryCat
  • 1,220
  • 2
  • 17
  • 32
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 04 '14 at 00:15
  • 1
    Can you show the assignment code? Are you using a specific framework? Soem frameworks do serialize dates this way. – Brian Mains Nov 04 '14 at 00:21
  • Are you trying to cast the DateTime as an Int anywhere in the javascript? That value is what you would get from the `Ticks` property of a DateTime – Jeffrey Wieder Nov 04 '14 at 00:29
  • @JeffreyWieder - No, I'm not doing any casting in the javascript – BinaryCat Nov 04 '14 at 00:32

2 Answers2

0

The conversion of the datetime value is causing your problem. Check out this related post on comparing a SQL datetime to a JavaScript date.

Edit: The value of "1415049596057" doesn't translate to a valid date. There's a quick check feature on datejs.com that you can validate the string value with.

Community
  • 1
  • 1
Lucas Fowler
  • 189
  • 2
  • 12
  • Misty - I figured that this issue has to do with conversion of the datetime value and I've already read several SO posts that are similar to the one you linked, but the format of the datetime value I have is quite different than all other examples, unless I'm missing something. – BinaryCat Nov 04 '14 at 00:49
  • I can't test it right now, and I'd need to see how your JavaScript is getting the DateTime value to know for sure, but are you converting the value multiple times in C#? And what is the type, when the JS gets ahold of it? When you convert a DateTime to a string, it's not automatically in a format that the JavaScript recognizes, so if it's a string, that's probably your problem. Try either typing it as a DateTime, or using a format string that JavaScrit recognizes. – Lucas Fowler Nov 04 '14 at 00:55
0

So, with a few more hours of rummaging through various SO posts, I finally found an answer here. As to why I'm getting the DateTime in that particular format, I'm still not sure 100%. But, I noticed some people refer to this odd-looking format as "JSON date", so maybe that's what it is but someone with a better grasp can confirm.

Anyway, for those who might be having the same issue, all I had to do was to add a single line of code in my Javascript as follows:

// 'dateVal' holds the datetime value I retrieved from the database. Also, since the value
// is in '/Date(1415049596057)/' format, I need to extract the numeric part of the value.
var date = new Date(parseInt(dateVal.substr(6)));

The value I have in the variable date is now Mon Nov 03 2014 16:19:56 GMT-0500 (Eastern Standard Time). As you can see, it's rather verbose but formatting that value to whatever format you want should be pretty straightforward.

Community
  • 1
  • 1
BinaryCat
  • 1,220
  • 2
  • 17
  • 32