Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Count day between Two label date

What I have tried:

//Count day between Two label date.

label1.Text = "2023,01,20";
DateTime aDate = DateTime.Now;
label2.Text = (aDate.ToString("yyyy,MM,dd"));
label3.Text = (label2.Text - label1.Text);
Posted
Updated 1-Mar-23 19:48pm

Don't use strings ... use DateTime values.
First, convert your string based date to a DateTime using the DateTime.TryParse Method[^] and allows you to tell the user when he enters a "bad date".
Then subtract the two dates and you will get a TimeSpan[^]. This has a TimeSpan.TotalDays Property[^] which gives you want you need.
 
Share this answer
 
private void countDate()
{
DateTime xmas = new DateTime(2018,12,25);
double daysUntilChristmas = DateTime.Today.Subtract(xmas).TotalDays;
label5.Text = Convert.ToString(daysUntilChristmas);
}


2nd.
dateTimePicker2.Value = DateTime.Now;
int daysDiff = ((TimeSpan)(dateTimePicker2.Value.Date - dateTimePicker1.Value.Date)).Days;
label5.Text = daysDiff.ToString();
 
Share this answer
 
v2
Comments
Richard Deeming 2-Mar-23 4:15am    
Check your variable names - yours claims to be the number of days until Christmas 2018, but the value will be the number of days since Christmas 2018.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900