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
.
Have a look at the DateTime.Date property.
Gets the date component of this instance.
in my experience none of the said solutions worked, maybe because I wanted to remove the time from extracted date from database, but the code below worked fine:
var date = target_date.Value.ToString("dd/MM/yyyy");
The easiest way is something like this and it will return only the date:
var date = DateTime.Now.ToShortDateString();
Use the Date property:
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
The date
variable will contain the date, the time part will be 00:00:00
.
If you are converting it to string, you can easily do it like this.
I'm taking date as your DateTime object.
date.ToString("d");
This will give you only the date.
Declare the variable as a string.
example :
public string dateOfBirth ;
then assign a value like :
dateOfBirth = ((DateTime)(datetimevaluefromDB)).ToShortDateString();