开发者

DateTime How To

When creating a class what is the syntax to define a public property开发者_如何学Python for that class as a DateTime type rather than string?


If you want a date object, with class-controlled formatting, you need two properties:

public DateTime DateField { get; set; }

// a read only string 
public String DateFieldString { 
    get { return DateField.ToString(/* your format */); } 
}


EDIT

ASP.NET: BoundField's DataFormatString property looks to be what you want.

<asp:BoundField 
       DataField="EventDate" 
       HeaderText="Event Date" 
       DataFormatString="{0:MM/dd/yyyy}"/>       

Easy as (pumpkin) pie. Happy Thanksgiving!


public class MyClass {
    public DateTime MyDate { get; set; }
    public string MyFormattedDate { get { return MyDate.ToString(myFormat); } }
}


Something like

    public class TestClas
    {
        DateTime dtDate;

        public DateTime DtDate
        {
            get
            {
                return dtDate;
            }
            set
            {
                dtDate = value;
            }
        }
    }

and to get and set the field you can use

TestClas objDate = new TestClas();

// set date
objDate.DtDate = DateTime.Now;

// get date
DateTime dtCurDate = objDate.DtDate;

Edit

It would be better not to implement the formatting inside the property. Make the formatting inside the gridview. Otherwise if you need another formatting then you would have to create another property.


using System;

public class Customer {

    private DateTime createDate;

    public DateTime CreateDate {
        get { return createDate; }
        set { createDate = value; }
    }

}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜