JBeckton
JBeckton

Reputation: 7111

DateTime How To

When creating a class what is the syntax to define a public property for that class as a DateTime type rather than string?

Upvotes: 1

Views: 312

Answers (5)

Mark Carpenter
Mark Carpenter

Reputation: 17775

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!

Upvotes: 2

rahul
rahul

Reputation: 187030

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.

Upvotes: 2

jason
jason

Reputation: 241641

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

Upvotes: 2

Michael Haren
Michael Haren

Reputation: 108246

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 */); } 
}

Upvotes: 3

Josh
Josh

Reputation: 69262

using System;

public class Customer {

    private DateTime createDate;

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

}   

Upvotes: 1

Related Questions