Format the TimeSpan string

I have a small C# program that has a calendar and 7 labels. When I select a date the labels display the days and dates of that week.

The labels are populated using the TimeSpan string what I want to do is format this string so that it only displays the days and dates with out the times.

This is the code I have so far:

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            DateTime dTime = new DateTime();
            dTime = monthCalendar1.SelectionStart;
            dTime -= new TimeSpan((int)dTime.DayOfWeek, 0, 0, 0 );

            for (int i = 1; i < 8; i++)
            {
                var dt = dTime.AddDays(i);
                lb[i].Text = dt.DayOfWeek + " : " + dt.Date;                         
            }           
        }

Answers


You have multiple options.

You can use ToShortDateString() method for the DateTime type

lb[i].Text = dt.DayOfWeek + " : " + dt.Date.ToShortDateString()

or you can provide of a format to the ToString("format") method to specify exactly what you want it to look like.

lb[i].Text = dt.DayOfWeek + " : " + dt.Date.ToString("MM/dd/yyyy");

You can call dt.Date.ToShortDateString().


Try with DateTime.ToShortDateString() method;

Converts the value of the current DateTime object to its equivalent short date string representation.

DateTime dt = DateTime.Now;
label8.Text = dt.Date.ToShortDateString());

You can learn more details from Custom Date and Time Format Strings


Need Your Help

Weblogic Spring Dependency Injection from flat-files on the file-system

spring dependency-injection weblogic

I have an application deployed into Oracle Weblogic 10.3. In my .ear files, I use spring injection from xml files in the ear files. I would like to also perform dependency injection from xml files

wordpress custom search for post under selected category

php wordpress search category custom-wordpress-pages

Hi I want to create a custom post search under a particular category. How can I create that.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.