Wednesday, 12 November 2014

Convert C# Datetime to Date in jquery


Controller
        public JsonResult Test()
        {
            return Json(DateTime.Now, JsonRequestBehavior.AllowGet);
        }


-------------------------------------------------------------------------------------------------------------------
View-Script

<script type="text/javascript">

        $.ajax({
            type: 'POST',
            contentType: "application/json",
            traditional: true,
            url: '../Home/Test',
            dataType: 'json',
            processData: false,
            success: function (res) {
                         
             
                var pattern = /Date\(([^)]+)\)/;
                var results = pattern.exec(res);
           
                var dt = new Date(parseFloat(results[1]));
                var n = dt.toLocaleDateString();
                alert(n);
                //var sd=(dt.getMonth()+1)  + "/" + dt.getDate() + "/" + dt.getFullYear();
                //alert(sd);
                var n1 = dt.toLocaleString();
                var s = n1.split(',')
                alert(s[0]);
            }
        });
<script>

No comments:

Post a Comment