Nikki
Nikki

Reputation: 137

Convert string to datetime and remove the time part from the date

I have a date returned from database which includes even the time. i want to remove the time part of the string and send only the date. my code is as given below

DateTime Var = new DateTime();
      Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
      Var = Var.ToShortDateString();

Upvotes: 0

Views: 6343

Answers (3)

Will Wang
Will Wang

Reputation: 537

you can custom your date string format by using DateTime.ToSting("your format") method. then the code will be like this.

  DateTime Var = new DateTime();
  Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
  Var = Var.ToString("yyyy-MM-dd");

you can also use SubString() method to gain the date part of the datetime string.

Upvotes: 0

hmnzr
hmnzr

Reputation: 1430

DateTime Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]).Date; //only date part
string date = Var.ToShortDateString();

Upvotes: 5

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

it will store only date in DateTime object

Var = Var.Date;

time will be 00:00:00

or you can store it as string:

var dateString = Var.ToShortDateString();

Upvotes: 3

Related Questions