Abstraction

Many beginners and sometimes experienced developers also encounter the question of how to compare two dates. In this article I have described this difference and explained everything that will help a beginner. If this has some drawback or if you have some suggestion, then feel free to mention it in the comments section. Today a friend told me that he encountered the same question in an interview during a machine test process.

Initial chamber

Step 1

Open Visual Studio and create an empty website, provide a suitable name such as DateCompare.

Step 2

In Solution Explorer you will get your empty website, then add web forms

For Web Form

DateCompare (your empty website). Right-click and select Add New Item, then Web Form. Name it DateCompare.aspx.

Design chamber

Step 3

Open the DateCompare.aspx file and write some code for the design of the application.

First add a jQuery plugin to your head section. Here I used jquery-1.9.1.js for demonstration purposes.

The following shows how to add it to your page:
  1. <div>
  2. <div>
  3. <table border="0" cellpadding="0" cellspacing="0">
  4. <tr>
  5. <th>First Date
  6. </th>
  7. <th>Second Date
  8. </th>
  9. </tr>
  10. <tr>
  11. <td>
  12. <asp:TextBox ID="txtDate1" runat="server" />
  13. </td>
  14. <td>
  15. <asp:TextBox ID="txtDate2" runat="server" />
  16. </td>
  17. <td>
  18. <asp:Button ID="btnDiff" runat="server" OnClick="btnDiff_Click" Text="Calculate Date" />
  19. </td>
  20. </tr>
  21. </table>
  22. </div>
  23. </div>
Now the design looks as in the following:

design

Now write some script for jQuery. Here we will write code to show and hide jQuery code:
  1. <script>
  2. $(document).ready(
  3. /* This is the function that will get executed after the DOM is fully loaded */
  4. function () {
  5. $("#txtDate1").datepicker({
  6. changeMonth: true,//this option for allowing user to select month
  7. changeYear: true //this option for allowing user to select from year range
  8. });
  9. $("#txtDate2").datepicker({
  10. changeMonth: true,//this option for allowing user to select month
  11. changeYear: true //this option for allowing user to select from year range
  12. });
  13. }
  14. );
  15. </script>
Add some CSS for the look and feel in the head section of the page:
  1. <style type="text/css">
  2. body {
  3. color: #333;
  4. text-align: center;
  5. background-color: aqua;
  6. }
  7. </style>
Now your page looks like the following:

DateCompare.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DateCompare.aspx.cs" Inherits="DateCompare" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>C# corner article for date comparision</title>
  6. <!-- Load jQuery from Google's CDN -->
  7. <!-- Load jQuery UI CSS -->
  8. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  9. <!-- Load jQuery JS -->
  10. <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  11. <!-- Load jQuery UI Main JS -->
  12. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  13. <!-- Load SCRIPT.JS which will create datepicker for input field -->
  14. <script>
  15. $(document).ready(
  16. /* This is the function that will get executed after the DOM is fully loaded */
  17. function () {
  18. $("#txtDate1").datepicker({
  19. changeMonth: true,//this option for allowing user to select month
  20. changeYear: true //this option for allowing user to select from year range
  21. });
  22. $("#txtDate2").datepicker({
  23. changeMonth: true,//this option for allowing user to select month
  24. changeYear: true //this option for allowing user to select from year range
  25. });
  26. }
  27. );
  28. </script>
  29. <style type="text/css">
  30. body {
  31. color: #333;
  32. text-align: center;
  33. background-color: aqua;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <form id="form1" runat="server">
  39. <div>
  40. <div>
  41. <table border="0" cellpadding="0" cellspacing="0">
  42. <tr>
  43. <th>First Date
  44. </th>
  45. <th>Second Date
  46. </th>
  47. </tr>
  48. <tr>
  49. <td>
  50. <asp:TextBox ID="txtDate1" runat="server" />
  51. </td>
  52. <td>
  53. <asp:TextBox ID="txtDate2" runat="server" />
  54. </td>
  55. <td>
  56. <asp:Button ID="btnDiff" runat="server" OnClick="btnDiff_Click" Text="Calculate Date" />
  57. </td>
  58. </tr>
  59. </table>
  60. </div>
  61. </div>
  62. </form>
  63. </body>
  64. </html>
On code behind page.

I have written some code for date comparison using C#.

DateCompare.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class DateCompare : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void CompareDate()
  13. {
  14. if (txtDate1.Text == "" && txtDate2.Text == "")
  15. {
  16. ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Textbox can not be blank!')", true);
  17. return;
  18. }
  19. else if (txtDate1.Text == "" || txtDate2.Text == "")
  20. {
  21. ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Date must be select for comparision')", true);
  22. return;
  23. }
  24. else
  25. {
  26. //here I have taken day part from textbox
  27. string date1Day = this.txtDate1.Text.Remove(2);
  28. string date2Day = this.txtDate2.Text.Remove(2);
  29. //here I have taken month part from textbox
  30. string date1Month = this.txtDate1.Text.Substring(3, 2);
  31. string date2Month = this.txtDate2.Text.Substring(3, 2);
  32. //here I have taken year part from textbox
  33. string date1Year = this.txtDate1.Text.Substring(6);
  34. string date2Year = this.txtDate2.Text.Substring(6);
  35. //here I have converting in datetime format
  36. DateTime d1 = Convert.ToDateTime(date1Month + "/" + date1Day + "/" + date1Year);
  37. DateTime d2 = Convert.ToDateTime(date2Month + "/" + date2Day + "/" + date2Year);
  38. if (d1 > d2)
  39. {
  40. ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('First dae is greater')", true);
  41. }
  42. else if (d1 == d2)
  43. {
  44. ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Both date are same')", true);
  45. }
  46. else
  47. {
  48. ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Second date is greater')", true);
  49. }
  50. }
  51. }
  52. protected void btnDiff_Click(object sender, EventArgs e)
  53. {
  54. CompareDate();
  55. }
  56. }
Output

On first text box
Figure 1: Initial loading

On second textbox click
Figure 2: On first text box

On button click when textbox is blanked
Figure 3: On second TextBox click

When one textbox is blank
Figure 4: On button click when TextBox is blank

When take a positive output
Figure 5: When one TextBox is blank

enter date
Figure: 6a


Compare date
Figure: 6b

date
Figure: 6c

Figure 6: When getting positive output

I hope you liked this. Have a good day. Thank you for reading.