Click here to Skip to main content
13,248,562 members (75,905 online)
Rate this:
 
Please Sign up or sign in to 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 7-Nov-17 21:20pm
Updated 8-Nov-17 5:19am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

            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();
  Permalink  
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 Bangalore 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 Bangalore 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
Karthik Bangalore 8-Nov-17 3:46am
   
What value you are getting in txtdate and txtdate 2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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;
}
  Permalink  
v6
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

 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();
    }
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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;
    }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 8 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100