简体   繁体   中英

.NET Date format in the database because of JSON conversion

I am trying get the same date format as it is the database like 12/2/2020 but the json is converting the date like /Date(1606845600000)/

I want to keep the date the same as it is the database. I just need the date portion not the time.

Any help would be highly appreciated.

Thanks

Here is the Model I am using

 public Item()
        {
            this.Inventories = new HashSet<Inventory>();
            this.PurchasesDetails = new HashSet<PurchasesDetail>();
            this.SalesDetails = new HashSet<SalesDetail>();
        }

        public int Id { get; set; }
        public string Code { get; set; }
        public int CategoryID { get; set; }
        public string Name { get; set; }
        public int MeasurementID { get; set; }
        public int Quantity { get; set; }
        public decimal BuyPrice { get; set; }
        public decimal SalePrice { get; set; }
        public decimal CommisionRate { get; set; }
        public Nullable<System.DateTime> MftDate { get; set; }
        public Nullable<System.DateTime> ExpDate { get; set; }
        public Nullable<int> StockLimit { get; set; }
        public string Description { get; set; }
        public string UserID { get; set; }

        public virtual AspNetUser AspNetUser { get; set; }
        public virtual Category Category { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Inventory> Inventories { get; set; }
        public virtual Measurement Measurement { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<PurchasesDetail> PurchasesDetails { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SalesDetail> SalesDetails { get; set; }
    }

Here is the code I am using

var itemz = new
        {
            item.Id,
           item.Code,
            item.Name,
            item.Quantity,
            item.BuyPrice,
            item.SalePrice,
            item.CommisionRate,
            item.CategoryID,
            item.UserID,
            item.MeasurementID,
            item.MftDate,
            item.ExpDate,
            item.StockLimit,
            item.Description
        };
        return Json(new { data = itemz }, JsonRequestBehavior.AllowGet);

here is the output

{
"data": {
"Id": 10,
"Code": "33",
"Name": "dd",
"Quantity": 44,
"BuyPrice": 45,
"SalePrice": 50,
"CommisionRate": 0,
"CategoryID": 6,
"UserID": "57e8c446-ccb7-4399-a7f3-898ccc8066fd",
"MeasurementID": 1,
"MftDate": "/Date(1606845600000)/",
"ExpDate": "/Date(1579543200000)/",
"StockLimit": 7,
"Description": null
}
}

You are defining the anonymous type in itemz. So you are managing that. If you want to keep some part of the date time in the json result. You only need to specify as a string for example in the desired format.

var itemz = new
    {
        item.Id,
       item.Code,
        item.Name,
        item.Quantity,
        item.BuyPrice,
        item.SalePrice,
        item.CommisionRate,
        item.CategoryID,
        item.UserID,
        item.MeasurementID,
        item.MftDate.ToString("dd/MM HH:mm:ss"),
        item.ExpDate.ToString("dd/MM/yyyy"),
        item.StockLimit,
        item.Description
    };
    return Json(new { data = itemz }, JsonRequestBehavior.AllowGet);

Here you have more information about .ToString method.

You should convert your date to the correct format.

As a result, you should change your code like below

var itemz = new
{
    item.Id,
    item.Code,
    item.Name,
    item.Quantity,
    item.BuyPrice,
    item.SalePrice,
    item.CommisionRate,
    item.CategoryID,
    item.UserID,
    item.MeasurementID,
    MftDate = item.MftDate == DateTime.MinValue ? "" : item.MftDate.Value.ToString("dd/MM/yyyy"),

    ExpDate = item.ExpDate == DateTime.MinValue ? "" : item.ExpDate.Value.ToString("dd/MM/yyyy"),
    item.StockLimit,
    item.Description
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM