How to format a time-stamp to show the date only in a grid view

前端 未结 5 2388
南方客
南方客 2021-02-19 20:27

In an aspx page I am binding the labels like this:

  
            
                &l         


        
5条回答
  •  执笔经年
    2021-02-19 20:51

    Create a FormatDate method in your codebehind, and call that from your gridview.
    http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx
    http://www.csharp-examples.net/string-format-datetime/

    This part will go in your code behind

    private object FormatDate(DateTime input)
    {
        return String.Format("{0:MM/dd/yy}", input);
    }
    

    And this bit will go in your markup

        
            
                
            
        
        
            
                
            
        
    

    This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.

提交回复
热议问题