ASP. net mvc handles the DateTime problem of JsonResult return time, jsonresultdatetime

Source: Internet
Author: User

ASP. net mvc handles the DateTime problem of JsonResult return time, jsonresultdatetime

When developing ASP. net mvc, if you use jQuery's Ajax to obtain some json data, the data returned contains the date and time, it may be a small problem.

To solve this problem, write a small example to demonstrate how to create a controller in the MVC project:


Write a view:



Let's take a look at this. When you display data in the view without any processing:

 
Modify the string "/Date (1435542121135)/" to take the digits in the brackets, convert it to int using the parseInt function, and pass it as a parameter of the new date object, in this way, new date is a new time object. :

 

Code:

Var obj = new Date (parseInt (data. result. substr (6); View Code


Or Replace the redundant characters with Replace:

 

Code:

Var obj = new Date (parseInt (data. result. replace ("/Date (",""). replace (")/", ""), 10); View Code


In this way, although the date and time information is displayed, it is in UTC format, but it is a bit unfriendly. We need to format it:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.