[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# ASP.Net date calculation

Posted on 2007-07-23
9
Medium Priority
?
255 Views
Last Modified: 2011-10-03
Hi Experts, I am using a c# inline code aspx with visual web developer 2005 express edition.
I have a text box to enter a date formated as '23/07/2007' pressing the button validates the date.
I am trying to print out the date entered in the text box to a label.
Example: if I enter 30/08/2007 in the text box and press the button, I want 30/08/2007 to be printed in the label. One more thing, once the above is achieved I want to calculate the number of days since 01/01/1900
to a seperate label. I am awarding 500 points as this is holding up a project I am working on.
Code so far as follows:
<%@ Page Language="C#"%>
<script runat=server>
void Button_Click(Object sender, EventArgs e)
{
    /// Response.Redirect( "ThankYou.aspx" );
    /// DateTime x = DateTime.now;
    /// string xStr = x.ToString("dd-MM-yyyy");
    /// lblText1.Text = xStr;
   
}
</Script>
<html>
<head><title>RegularExpressionValidator.aspx</title></head>
<body>
<form id="Form1" Runat="Server">
Enter Date in 23/07/2007 format:
<br>
<asp:TextBox
  id="txtDateCode"
  Runat="Server"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
  ControlToValidate="txtDateCode"
  Text="Invalid Product Code!"
  ValidationExpression="\d{2}/\d{2}/\d{4}"
  Runat="Server" />
<p>
<asp:Button ID="Button1"
  Text="Submit"
  OnClick="Button_Click"
  Runat="Server"/>
    <asp:Label ID="lblText1" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
0
Comment
Question by:homeshopper
  • 4
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19546394
Difference in days between 2 days:

void Button_Click(Object sender, EventArgs e)
{
        DateTime d1 = Convert.ToDateTime(TextBox1.Text);
        DateTime d2 = Convert.ToDateTime(TextBox2.Text);
        TimeSpan elapsed = d2.Subtract(d1);
        Label1.Text = elapsed.Days.ToString();
}
0
 
LVL 10

Expert Comment

by:SystemExpert
ID: 19546446
private void button3_Click(object sender, System.EventArgs e)
{
      DateTime EndDate=Convert.ToDateTime("01/01/1900") ;
      DateTime  startDate ;
      lblDisplay.Text=Convert.ToDateTime(txtInput.Text).ToString("dd/MM/yyyy") ;
      startDate = Convert.ToDateTime(txtInput.Text) ; ;
      TimeSpan span = startDate.Subtract ( EndDate );
      lblDays.Text = "Days " + span.Days ;
}
0
 

Author Comment

by:homeshopper
ID: 19546625
Thank you for a very quick response.
I'll try out both solutions and let you know how I get on.
Thanks in advance.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Dhaest
ID: 19563927
Did you try it ? Still having problems ?
0
 

Author Comment

by:homeshopper
ID: 19569150
Hi Dhaest,
Sorry to not have got back to you sooner.
I have however been experimenting with both solutions as follows:
<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click1(object sender, EventArgs e)
    {
        DateTime EndDate = Convert.ToDateTime("01/01/1900");
        DateTime startDate;
        lblDisplay.Text = Convert.ToDateTime(txtInput.Text).ToString("dd/MM/yyyy");
        startDate = Convert.ToDateTime(txtInput.Text); ;
        TimeSpan span = startDate.Subtract(EndDate);
        lblDays.Text = "Days " + span.Days;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        DateTime d1 = Convert.ToDateTime(TextBox3.Text);
        DateTime d2 = Convert.ToDateTime(TextBox4.Text);
        TimeSpan elapsed = d2.Subtract(d1);
        Label2.Text = elapsed.Days.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
    <asp:Label ID="lblDisplay" runat="server" Text="Label" Width="122px"></asp:Label>
    <asp:Label ID="lblDays" runat="server" Text="Label" Width="132px"></asp:Label>
    <br />
    <br />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    <asp:Label ID="Label2" runat="server" Text="Label" Width="122px"></asp:Label><br />
    <br />
</div>
</form>
</body>
</html>
I have some test data to try out,I'll let you know how I get on.
Using either of the two solutions, when calculating the difference between the two dates,
Do I need to alter the function to allow for leap years?
0
 

Author Comment

by:homeshopper
ID: 19638848
Hi Experts, I have been doing a number of experiments and have produced the code as per listing below:
The code so far works, when you press a button after entering a date, it calculates the number of days since 1st Jan 1900. If you put two different dates i.e startdate & enddate I would like to be able to subtract the number of days in startdate from the number of days in enddate, then output the result to a label called "lblDays". Thanks in advance.

<%@ Page Language="C#" Debug="true" Trace="False"%>
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();
        }
        else
        {
         
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        long years = 0;
        years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
        years = (years - 1900) * 365;
        Response.Write(years + "days + ");
        long months = 0;
        months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
        months = months * 30;
        Response.Write(months + "days + ");
        long days = 0;
        days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
        Response.Write(days + "days = ");
        long TotalDays = 0;
        TotalDays = years + months + days;
        Response.Write(TotalDays + "Total days");
       
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        long years2 = 0;
        years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
        years2 = (years2 - 1900) * 365;
        Response.Write(years2 + "days + ");
        long months2 = 0;
        months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
        months2 = months2 * 30;
        Response.Write(months2 + "days + ");
        long days2 = 0;
        days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
        Response.Write(days2 + "days = ");
        long TotalDays2 = 0;
        TotalDays2 = years2 + months2 + days2;
        Response.Write(TotalDays2 + "Total days");

        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
        ///long TotalDays3 = 0;
        ///TotalDays3 = TotalDays2 - TotalDays;
        ///lblDays.Text = Convert.ToString(output3);
        ///lblDisplay.Text = Convert.ToString("DD/MM/YYY");
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
<asp:Label ID="lblDays" runat="server" Text="Label" Width="132px"></asp:Label>
</form>
</body>
</html>
0
 

Author Comment

by:homeshopper
ID: 19679324
Hi experts, I would like to conclude this question and award points.
The solution and code is as below, with help from bob thelearnedone & Dhaest:
<%@ Page Language="C#" Debug="true" Trace="False"%>
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        long years = 0;
        long years2 = 0;
        long months2 = 0;
        long months = 0;
        long days2 = 0;
        long days = 0;
        long TotalDays = 0;
        long TotalDays2 = 0;
        long TotalDays3 = 0;
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();

            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
           
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
           
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
           
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
           
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
           
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
        }
        else
        {
            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
            years = (years - 1900) * 365;
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
            months = months * 30;
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
            years2 = (years2 - 1900) * 365;
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
            months2 = months2 * 30;
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
            TotalDays = years + months + days;
            TotalDays2 = years2 + months2 + days2;
            TotalDays3 = TotalDays2 - TotalDays;
            Response.Write("TotalDays:");
            Response.Write("#");
            Response.Write(TotalDays3);
            Response.Write("#");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
    <br />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
    <br />
    <br />
<asp:Label runat="server" id="output3" />
</form>
</body>
</html>
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 19933590
Split points between ThelearnedOne & Dhaest
As stated in last post from asker:
>> The solution and code is as below, with help from bob thelearnedone & Dhaest
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question