I need to remove time portion of date time or probably have the date in following format in object
form not in the form of string
.
The Date
property will return the date at midnight.
One option could be to get the individual values (day/month/year) separately and store it in the type you want.
var dateAndTime = DateTime.Now;
int year = dateAndTime.Year;
int month = dateAndTime.Month;
int day = dateAndTime.Day;
string.Format("{0}/{1}/{2}", month, day, year);
You can't. A DateTime in .NET always have a time, defaulting to 00:00:00:000. The Date property of a DateTime is also a DateTime (!), thus having a time defaulting to 00:00:00:000 as well.
This is a shortage in the .NET Framework, and it could be argued that DateTime in .NET violates the Single Responsibility Principle.
This could be simply done this way:
var dateOnly = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day)
To get only the date portion use the ToString() method,
example: DateTime.Now.Date.ToString("dd/MM/yyyy")
Note: The mm in the dd/MM/yyyy format must be capitalized
Use a bit of RegEx:
Regex.Match(Date.Now.ToString(), @"^.*?(?= )");
Produces a date in the format: dd/mm/yyyy
Use the method ToShortDateString. See the documentation http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
var dateTimeNow = DateTime.Now; // Return 00/00/0000 00:00:00
var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000