Date Calculator using C#

This is a simple tutorial in which we will create a simple Date Calculator program made in C#. On this, it will help you know the dates before and after the present date. For instance, you want to jump into seven days after the present date, the date calculator will automatically calculate it. And you don’t have to count the days anymore, so the user will benefit this kind of program. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add two textboxes, the first is “txtafter” and the other one is “txtbefore” and the DateTimePicker is “dtpstrdate” and one listbox named ListBox1.You must design your interface like this: output 3. Put the following code below in your Form_Load.
  1. //declaring the variables
  2. int intervaldate; //represents a date interval
  3. DateTime dateresult; //represents the added date of start date and the interval days
  4. string msg; //repesents a message to put into a listbox
4. Have the code below for txtafter_TextChanged, put this code below.
  1. private void txtafter_TextChanged(System.Object sender, System.EventArgs e)
  2. {
  3. try
  4. {
  5. //conditioning the after days interval
  6. if (txtafter.Text.Length > 0) //check the interval days if greater than 0.
  7. {
  8. intervaldate = system.convert.toint32(txtafter.text); //assigning a textbox to a varialble
  9. //formula
  10. dateresult = dtpstrdate.Value.AddDays(intervaldate).ToString(); //adding the interval days to the start date.
  11. msg = "After " + intervaldate + " day(s), the date is "; //message to be put in the listbox
  12. ListBox1.Items.Clear(); //clearing a listbox
  13. ListBox1.Items.Add(msg + Format(dateresult, "dddd, MMMM dd, yyyy")); //adding a message and formatted date result in the listbox
  14. txtbefore.Clear(); //clearing the textbox
  15. }
  16. else
  17. {
  18. ListBox1.Items.Clear(); //clearing a listbox
  19. }
  20. }
  21. catch (Exception ex)
  22. {
  23. MessageBox.Show((string) ex.Message);
  24. }
  25.  
  26.  
  27. }
5. Have the code below for txtbefore_TextChanged, put this code below.
  1. private void txtbefore_TextChanged(System.Object sender, System.EventArgs e)
  2. {
  3. try
  4. {
  5. //conditioning the before days interval
  6. if (txtbefore.Text.Length > 0)
  7. {
  8. intervaldate = System.Convert.ToInt32(txtbefore.Text);
  9. //formula
  10. //syntax...
  11. //the formula is the same but the twist is
  12. //i add a negative interval so that it will go back to the previous date.
  13. dateresult = dtpstrdate.Value.AddDays(- intervaldate).ToString();
  14. msg = "Before " + intervaldate + " day(s), the date is ";
  15. ListBox1.Items.Clear();
  16. ListBox1.Items.Add(msg + Format(dateresult, "dddd, MMMM dd, yyyy"));
  17. txtafter.Clear();
  18. }
  19. else
  20. {
  21. ListBox1.Items.Clear();
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. MessageBox.Show((string) ex.Message);
  27. }
  28. }

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment