Kamran
Kamran

Reputation: 4100

How to select only data part from a list

I am experimenting with lists. I have a list which is getting data from the DataTable dt_text_global:

List<DateTime> Ersch_Online = dt_text_global
                                 .AsEnumerable()
                                 .Select(r => r.Field<DateTime>("Ersch_Online"))
                                 .ToList();

Data in DataTable dt_text_global contains only date and no time.

when I use it like this e.g.

TextBox1.Text = Ersch_Online[1].Tostring();

It prints as 14.01.2014 00:00:00. But I only want to show date not the time.

What I have to do ??

Upvotes: 1

Views: 83

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

If you want to show only date, then use ToShortDateString() or provide custom format string:

TextBox1.Text = Ersch_Online[1].ToShortDateString();
// or
TextBox1.Text = Ersch_Online[1].ToString("d");
// or
TextBox1.Text = Ersch_Online[1].ToString("dd.MM.yyyy");

If you want to show only time part, then use ToShortTimeString() or provide custom format:

TextBox1.Text = Ersch_Online[1].ToShortTimeString();
// or
TextBox1.Text = Ersch_Online[1].ToString("t");
// or
TextBox1.Text = Ersch_Online[1].ToString("HH:mm:ss");

Upvotes: 4

Plue
Plue

Reputation: 1790

You can specify the format in the ToString() method :

TextBox1.Text = Ersch_Online[1].ToString("yyyy.MM.dd");

Here are the different formats.

Upvotes: 0

Habib
Habib

Reputation: 223277

Since you edited the question and now you need to show Date not time. You can do:

TextBox1.Text = Ersch_Online[1].ToShortDateString();

OLD Answer

But I only want to show time not the date.

If you only want to show Time then use TimeOfDay property to just get the Time part from DateTime. Like:

List<TimeSpan> Ersch_Online = dt_text_global
                     .AsEnumerable()
                     .Select(r => r.Field<DateTime>("Ersch_Online").TimeOfDay) 
                     .ToList();

Upvotes: 4

Related Questions