Three methods to solve the json Date Format problem _ json-js tutorial

Source: Internet
Author: User
This article mainly introduces three methods to solve the json Date Format problem. For more information, see the following, in the background code, if data of the DateTime type is serialized using the tool class that comes with the system, a long number will be obtained to represent the date data, as shown below:

The Code is as follows:


// Set the server response result to plain text format
Context. Response. ContentType = "text/plain ";
// Student object set
List Students = new List
{
New Student () {Name = "Tom ",
Birthday = Convert. ToDateTime ("12:12:12 ")},
New Student () {Name = "Rose ",
Birthday = Convert. ToDateTime ("11:12:12 ")},
New Student () {Name = "Mark ",
Birthday = Convert. ToDateTime ("10:12:12 ")}
};

// Javascript serializer
JavaScriptSerializer jss = new JavaScriptSerializer ();
// Serialize the student collection object to get json characters
String studentsJson = jss. Serialize (students );
// Return the string to the client
Context. Response. Write (studentsJson );
Context. Response. End ();

The running result is:

Among them, Tom's birthday "January 1, 1970" has changed to 1391141532000, which is actually the number of milliseconds since 1391141532000/1000; 60/60/24/365/44.11 = 1970 years, 44 + =, in this way, we can obtain the year, month, day, hour, minute, second, and millisecond. This format is a feasible representation, but not a friendly one that ordinary people can understand. How can this format be changed?

Solution:

Method 1: on the server side, convert the date format using the Select method or LINQ expression and send it to the client:

The Code is as follows:


Using System;
Using System. Collections. Generic;
Using System. Web;

Using System. Web. Script. Serialization;

Namespace JsonDate1
{
Using System. Linq;

///


/// Student type for test
///
Public class Student
{
///
/// Name
///
Public String Name {get; set ;}

///


/// Birthday
///
Public DateTime Birthday {get; set ;}
}

///


/// Return the json character of the student set
///
Public class GetJson: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
// Set the server response result to plain text format
Context. Response. ContentType = "text/plain ";
// Student object set
List Students = new List
{
New Student () {Name = "Tom", Birthday = Convert. ToDateTime ("12:12:12 ")},
New Student () {Name = "Rose", Birthday = Convert. ToDateTime ("11:12:12 ")},
New Student () {Name = "Mark", Birthday = Convert. ToDateTime ("10:12:12 ")}
};

// Use the Select method to re-project the object set to convert the Birthday attribute to a new one.
// Remember to rename the attribute after it changes and execute it immediately
Var studentSet =
Students. Select
(
P => new {p. Name, Birthday = p. Birthday. ToString ("yyyy-mm-dd ")}
). ToList ();

// Javascript serializer
JavaScriptSerializer jss = new JavaScriptSerializer ();
// Serialize the student collection object to get json characters
String studentsJson = jss. Serialize (studentSet );
// Return the string to the client
Context. Response. Write (studentsJson );
Context. Response. End ();
}

Public bool IsReusable
{
Get
{
Return false;
}
}
}
}


The Select method re-projects the object set to convert the Birthday attribute into a new attribute. Note that the attribute must be renamed after the attribute changes. The attribute names can be the same; here, you can use the select method or the LINQ query expression, or you can select another method to achieve the same purpose. This method can remove the attributes not used by the client in the collection, to achieve simple performance optimization.

Running result:

At this time, the date format has changed to a friendly format, but in javascript this is only a string.

Method 2:

In javascript, convert the string in "Birthday": "\/Date (1391141532000) \/" to a Date object in javascript, you can delete the non-numeric characters in the Value corresponding to the Key Birthday in the form of replacement, To a number 1391141532000, and then instantiate a Date object, 1391141532000 milliseconds as the parameter, get a date object in javascript, the Code is as follows:

The Code is as follows:





Json date format processing

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.