简体   繁体   中英

How Can I Convert Json Datetime To Normal Date Time Format(dd.mm.yyyy)?

How can I convert a Json DateTime string to normal date format(dd.mm.yyyy)?

    public JsonResult ProductListGrid()
    {
        try
        {
            var productListModel = new List<ProductListModel>();

            var products= ProductOperations.ProductListGetirDT(ExceptionCatcher);
            foreach (var item in products)
            {
                productListModel.Add(new ProductListModel()
                {
                    //...
                    ProductDate = Convert.ToDateTime(item.ProductDate ),
                   //:::
                });
            }

            return Json(productListModel , JsonRequestBehavior.AllowGet);
        }
        ...

17.12.2018 (I expect)

/Date(1549659600000)/ (Output)

You can try this:

time = datetime.datetime.strptime(ProductDate, '%Y-%m-%d %H:%M:%S.%f')  

You can use TryParseExact to convert the string date to DateTime

private static DateTime ParseDate(string providedDate)
{
    DateTime validDate;
    string[] formats = { "dd.MM.yyyy" };
    var dateFormatIsValid = DateTime.TryParseExact(
        providedDate,
        formats,
        CultureInfo.InvariantCulture,
        DateTimeStyles.None,
        out validDate);
    return dateFormatIsValid ? validDate : DateTime.MinValue;
}

And call this method to parse the json date format

ProductDate = ParseDate(item.ProductDate)

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