-2

I save UTC time on the server, and when I pass it to the front end it gives me something like this: "/Date(1543396029630)/"

My question is: is this the int the utc timestamp?

I tried to convert the int using an online converter, but the result is different from my record in the database.

My database record is "28/11/2018 23:12:33"

public ActionResult test()
{
    var date = _context.Batches.FirstOrDefault(c => c.Batches_BatchID == 1)
        .Batches_ClosedDate;

    var dateString = _context.Batches.FirstOrDefault(c => c.Batches_BatchID == 1)
        .Batches_ClosedDate.ToString();

    return Json(new
        {
            dateString,
            date,
        }, JsonRequestBehavior.AllowGet);
}

and the result is:

{
"dateString": "28/11/2018 23:12:33",
"date": "/Date(1543399953230)/"
}

and I went to an online converter, it gives me the date is different from dateString any idea?

My account got blocked by some down votes questions, the funny thing is I have to re-edit them, even though I already have the accepted answer.I do not understand what's the point to do this.I am so frustrated by this stackoverflow system.

Now, I basically can do nothing but keep editing my questions, and they have all been answered. This is ridiculous !!!

Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • *when I passed it to front* could you give some example how did you render it in front? – Bagus Tesa Nov 28 '18 at 22:56
  • @BagusTesajust standard mvc. – Andy Song Nov 28 '18 at 22:57
  • `"dateString": "28/11/2018 23:12:33",` looks nothing changed from the database entry. do double check for your frontend code that converts the date.. its hard to help if you dont help us providing [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example of your problem.. or at least, give us insight what this frontend based on... :( – Bagus Tesa Nov 29 '18 at 01:09

1 Answers1

2

1543396029630 is the Unix time milliseconds format representation of:

Wednesday, 28-Nov-18 09:07:09 UTC

You can convert it into a Date in javascript like this:

var date = new Date(unix_timestamp_in_milliseconds);

Since you are getting a different time than you expect, there are a bunch of things that could be going on, related to:

  1. the DateTime.Kind propery on your date instance, and
  2. the settings your Json serializer is using (e.g. if you are using Json.NET, then the JsonSerializerSettings.DateTimeZoneHandling and JsonSerializerSettings.DateFormatHandling properties will be relevant.

See the Json.Net documentation for a guide to how these interact.

I would personally recommend formatting your datetime using ISO-8601 format, and parsing that to the Date() in your front end code. You can achieve this by either explicitly formatting the string in your own code, or configuring your Json serializer to do it automatically.

If you are using the default Json.Net serializer (in ASP.NET core), then you would configure the serializer using using something like this in your Startup class (untested - just to give you the idea):

services.AddMvc()
    .AddJsonOptions(opt =>
        {
            opt.SerializerSettings.DateFormatHandling =
                DateFormatHandling.IsoDateFormat;
            opt.SerializerSettings.DateTimeZoneHandling =
                DateTimeZoneHandling.Unspecified
           // Or maybe DateTimeZoneHandling.Utc - you should test!
        });
Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • however,my database record is "28/11/2018 22:07:09", does that mean my database record is not correct? – Andy Song Nov 28 '18 at 22:59
  • my confusion is that after I convert Unix time ticks to utc time and compare to the record in database, they are different. in C# I used `DateTime.UtcNow;` to generate the datetime. – Andy Song Nov 28 '18 at 23:05
  • Yes, it looks like your database record probably isn't UTC. Either that, or it is being manipulated before being converted into a unix timestamp. You can use another tool to double check your conversions, e.g.: https://currentmillis.com/ – Ergwun Nov 28 '18 at 23:05
  • hi, I just edited my post can you please have a look, what I did wrong? – Andy Song Nov 28 '18 at 23:37
  • @AndySong I've updated the answer with some suggestions about why you may be getting the wrong value. – Ergwun Nov 29 '18 at 04:02