Custom output date format of StringTemplate in asp.net

Source: Internet
Author: User

Use additional renderers like this class to implement the interface:

The code is as follows: Copy code
Internal class AdvancedDateTimeRenderer: IAttributeRenderer
{
Public string ToString (object o)
    {
Return ToString (o, null );
    }
 
Public string ToString (object o, string formatName)
    {
If (o = null)
Return null;
 
If (string. IsNullOrEmpty (formatName ))
Return o. ToString ();
 
DateTime dt = Convert. ToDateTime (o );
 
Return string. Format ("{0:" + formatName + "}", dt );
    }
}

And then add this to your StringTemplate such as register the class to the template group:

The code is as follows: Copy code
Var stg = new StringTemplateGroup ("Templates", path );
Stg. RegisterAttributeRenderer (typeof (DateTime), new AdvancedDateTimeRenderer ());

Statement in the then in st file Template file:

The code is as follows: Copy code

$ YourDateVariable; format = "dd/mm/yyyy" $

The other method calls the attribute. ToString () method by default. To customize the output format, you must implement the IAttributeRenderer interface. The following defines a custom date formatting output:

The code is as follows: Copy code

Class CustomDataRenderer: IAttributeRenderer {
             
Public string ToString (object o)
    {
If (o = null)
Return null;
         
String str = o. ToString ();
         
DateTime dt;
If (! DateTime. TryParse (o. ToString (), out dt ))
Return str;
         
Return dt. ToString ("MM dd, yyyy, HH: mm ");
    }
     
Public string ToString (object o, string formatName)
    {
Throw new NotImplementedException ();
    }
}

Usage:

The code is as follows: Copy code

StringTemplate st = new StringTemplate ("$ d $ ");
St. RegisterRenderer (typeof (DateTime), new CustomDataRenderer (); // register the default output format of the DateTime type in the current Template
St. SetAttribute ("d", DateTime. Now );
Console. WriteLine (st. ToString ());


We can see that the format defined by the previously defined ToString (object o) method is output. If multiple formats need to be output, this method cannot be used.

Next, modify the CustomDataRenderer class so that it can output multiple formats:

The code is as follows: Copy code


/// <Summary>
/// Output the custom ST date format
/// </Summary>
Class CustomDataRenderer: IAttributeRenderer {
     
/// <Summary>
/// Default output method
/// </Summary>
Public string ToString (object o)
    {
Return ToString (o, null );
    }
     
/// <Summary>
/// Output method when the output format is specified
/// </Summary>
/// <Param name = "formatName"> format name </param>
Public string ToString (object o, string formatName)
    {
If (o = null)
Return null;
         
String str = o. ToString ();
         
DateTime dt;
If (! DateTime. TryParse (o. ToString (), out dt ))
Return str;
         
Switch (formatName ){
Case "CNDate ":
Return dt. ToString ("MM dd, yyyy ");
Case "CNTime ":
Return dt. ToString ("HH: mm ");
Default:
Return dt. ToString ("MM dd, yyyy, HH: mm ");
        }
    }
}

Usage:

StringTemplate st = new StringTemplate ("[$ d1 $] [$ d2; format =" CNDate "$] [$ d3; format =" CNTime "$]");
St. RegisterRenderer (typeof (DateTime), new CustomDataRenderer (); // register the default output format of the DateTime type in the current Template
St. SetAttribute ("d1", DateTime. Now );
St. SetAttribute ("d2", DateTime. Now );
St. SetAttribute ("d3", DateTime. Now );
Console. WriteLine (st. ToString ());

Output: [, January 1, April 30, 2010] [, April 30, 2010]

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.