Jun 14, 2011

How to get only date value in C#

There is no date type in .Net, only DateTime.

To get only date you can do one of the following:


Code:




// Get current date and time
DateTime dt=DateTime.Now;


// Get year, month, and day
int year=dt.Year;
int month=dt.Month;
int day=dt.Day;


// Get datetime at midnight
DateTime date=dt.Date;


// Get long date string
string longDate=dt.ToLongDateString();


// Get short date string
string shortDate=dt.ToShortDateString();


// Get Ole Automation Date (number of days from midnight, 30 December 1899)
double oaDate=dt.ToOADate();

No comments:

Post a Comment