MVC日期格式化,后台使用Newtonsoft.Json序列化日期,前端使用”f”格式化日期

时间:2024-01-09 22:01:38

MVC控制器中,经常使用Newtonsoft.Json把对象序列化成json字符串传递到前端视图。当对象中有DateTime类型的属性时,前后台如何处理才能把DateTime类型转换成想要的格式呢?

 

有这样的一个类具有DateTime类型属性:

using System;
 
namespace MvcApplication1.Models
{
    public class Sample
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Date { get; set; }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

控制器中使用Newtonsoft.Json把对象实例序列化成json字符串:

using System.Web.Mvc;
using MvcApplication1.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var sample = new Sample()
            {
                Id = 1,
                Name = "good",
                Date = DateTime.Now
            };
 
            JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
            {
                 DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                 DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };
            ViewData["json"] = Newtonsoft.Json.JsonConvert.SerializeObject(sample, microsoftDateFormatSettings);
            return View();
        }
 
    }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

前台视图使用一个叫"f"的js文件,把json中的日期格式转换成期望的样子:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>
    <span id="jsonDate">@ViewData["json"]</span>
</p>
 
<p>
    转换后的时间格式为:<span id="fDate"></span>
</p>
 
@section scripts
{
    <script src="~/Scripts/date.f-0.5.0.js"></script>
    <script type="text/javascript">
        $(function () {
           
            var obj = $.parseJSON($('#jsonDate').text());
            //$('#fDate').text(new Date(parseInt(obj.Date.substr(6))).f("yyyy-MM-dd HH:mm:ss"));
            $('#fDate').text(new Date(parseInt(obj.Date.substr(6))).f("yyyy-MM-dd"));
        });
    </script>
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结果:

MVC日期格式化,后台使用Newtonsoft.Json序列化日期,前端使用”f”格式化日期

 

参考资料:

关于"f" 的js文件:http://fisforformat.sourceforge.net/

关于Newtonsoft.Json:http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm