Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello 
 
My Requirment is as following
Printing all the dates between first date & Second date
 .aspx--------------
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="date-picker.aspx.cs" Inherits="date_picker" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<title>jQuery UI Datepicker - Change Date Format Example



$(function () {
$("#txtDate").datepicker({ dateFormat: 'yy-mm-dd' });
});


$(function () {
$("#txtDate2").datepicker({ dateFormat: 'yy-mm-dd' });
});


.ui-datepicker
{
font-size: 8pt !important;
}




<div class="demo">
Date 1:
<asp:TextBox ID="txtDate" runat="server" />
<br><br>
Date 2:
<asp:TextBox ID="txtDate2" runat="server" />
<br>
<br>
<asp:Button ID="printdate" runat="server" Text="Print Dates"
onclick="printdate_Click" />
</div>



. cs------------------------- 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class date_picker : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void printdate_Click(object sender, EventArgs e)
{
}
}


What I have tried:

Actually My requrment is to print all the dates from the selected range of date in asp.net c#.
urgently Looking fir hlep
Posted
Updated 8-Nov-17 4:19am
v3

DateTime fromDate;
DateTime toDate;
if (!DateTime.TryParseExact(txtDate.Text.Trim(), "yy-mm-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out fromDate))
{
    // throw error message, saying invalid date format
}

if (!DateTime.TryParseExact(txtDate2.Text.Trim(), "yy-mm-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out toDate))
{
    // throw error message, saying invalid date format
}

DataTable dt = new DataTable();
dt.Columns.Add("Dates");


if (fromDate > toDate)
{
    // throw error message, saying from date should be lesser than end date
}
else
{
    double totaldays = (toDate - fromDate).TotalDays;
    for (int i = 0; i < totaldays; i++)
        dt.Rows.Add(fromDate.AddDays(i).ToString("yy-mm-dd"));

}

GridView1.DataSource = dt;  // display the data in the gridview.
GridView1.DataBind();
 
Share this answer
 
Comments
Vivek VIshwas 8-Nov-17 3:11am    
The above code is not generating the date please check Carefully. and update me. Thank You
Karthik_Mahalingam 8-Nov-17 3:16am    
Debug the code and see where you are getting the error
Vivek VIshwas 8-Nov-17 3:19am    
the datatable you have used to store the "dates", that does not contains any data.
Please review the code.
Karthik_Mahalingam 8-Nov-17 3:22am    
What is the value you are getting in fromdate and todate
Vivek VIshwas 8-Nov-17 3:24am    
nothing because both variable doesn't holds any date

Test solution

in reference to Bugs and Suggestions[^]
_______________________________________________________________________________________


public void printdate1()
{
DateTime StartingDate = DateTime.Parse(txtDate.Text);

DateTime EndingDate = DateTime.Parse(txtDate2.Text);
foreach (DateTime date in GetDateRange(StartingDate, EndingDate))
{
//WL(date.ToShortDateString());

Response.Write("'"+ date.ToShortDateString() +"' ");

//Response.Write("alert('" + date.ToShortDateString() + "') ");
//lbldate.InnerHtml = date.ToShortDateString();

}
}
private List<datetime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
if (StartingDate > EndingDate)
{
return null;
}
List<datetime> rv = new List<datetime>();
DateTime tmpDate = StartingDate;
do
{
rv.Add(tmpDate);
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
}
 
Share this answer
 
v6
protected void printdate_Click(object sender, EventArgs e)
   {
       //    DateTime starting = new DateTime();
       //    starting = DateTime.ParseExact(txtDate.Text, "yyyy-MM-dd", null);
       //    DateTime ending = new DateTime();
       //    ending = DateTime.ParseExact(txtDate2.Text, "yyyy-MM-dd", null);

       // //   List<DateTime> dates = GetDatesBetween(starting, ending);
       //    DateTime[] dates = GetDatesBetween(starting, ending).ToArray();

       //}
       printdate1();
   }


  public void printdate1()
   {
       DateTime fromDate;
       DateTime toDate;
       if (!DateTime.TryParseExact(txtDate.Text.Trim(), "yy-mm-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out fromDate))
       {
           // throw error message, saying invalid date format
       }

       if (!DateTime.TryParseExact(txtDate2.Text.Trim(), "yy-mm-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out toDate))
       {
           // throw error message, saying invalid date format
       }

       DataTable dt = new DataTable();
       dt.Columns.Add("Dates");


       if (fromDate > toDate)
       {
           // throw error message, saying from date should be lesser than end date
       }
       else
       {
           double totaldays = (toDate - fromDate).TotalDays;
           for (int i = 0; i < totaldays; i++)
               dt.Rows.Add(fromDate.AddDays(i).ToString("yy-mm-dd"));

       }

       GridView1.DataSource = dt;  // display the data in the gridview.
       GridView1.DataBind();
   }
 
Share this answer
 
Comments
Vivek VIshwas 8-Nov-17 3:40am    
where is your improvement code...
BillWoodruff 8-Nov-17 6:29am    
Please do not post edits of your original post as solutions. Instead, modify/revise/update your original post.
C#
public void printdate1()
    {
        DateTime StartingDate = DateTime.Parse(txtDate.Text);
        
        DateTime EndingDate = DateTime.Parse(txtDate2.Text);
        foreach (DateTime date in GetDateRange(StartingDate, EndingDate))
        {
            //WL(date.ToShortDateString());
            
            Response.Write("'"+ date.ToShortDateString() +"' ");
            
            //Response.Write("<script>alert('" + date.ToShortDateString() + "')  </script>");
            //lbldate.InnerHtml = date.ToShortDateString();

        }
    }
    private List<datetime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
    {
        if (StartingDate > EndingDate)
        {
            return null;
        }
        List<datetime> rv = new List<datetime>();
        DateTime tmpDate = StartingDate;
        do
        {
            rv.Add(tmpDate);
            tmpDate = tmpDate.AddDays(1);
        } while (tmpDate <= EndingDate);
        return rv;
    }
 
Share this answer
 
v2
Comments
CHill60 8-Nov-17 6:57am    
Is this a solution to your own question?
Vivek VIshwas 8-Nov-17 23:18pm    
yes

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