This is Query which i can get all record which is fall between in both dates.
SELECT * FROM tblBooking WHERE
Convert(datetime,'2013-08-20 04:00:00.000') --start date
BETWEEN FromDateWithStartTime AND ToDateWithEndTime
OR Convert(datetime,'2013-08-30 04:00:00.000') --endDate
BETWEEN FromDateWithStartTime AND ToDateWithEndTime
or FromDateWithStartTime BETWEEN Convert(datetime,'2013-08-20 04:00:00.000') --startdate
AND Convert(datetime,'2013-08-30 04:00:00.000') --enddate
or ToDateWithEndTime BETWEEN Convert(datetime,'2013-08-20 04:00:00.000')--start date
AND Convert(datetime,'2013-08-30 04:00:00.000')
and in C# i an fetching start Date and End Date.
FromDateWithStartTime =
Convert.ToDateTime(dt.Rows[0]["Fdate"]).ToString("yyyy/MM/dd
HH:mm:ss");
ToDateWithEndTime = Convert.ToDateTime(dt.Rows[0]["Edate"]).ToString("yyyy/MM/dd
HH:mm:ss");
I want those date which is fall between START DATE AND END DATE.
AND BIND ALL DATES IN THE DRP DOWN LIST.
HOW I CAN DO IT.
you can just iterate through your query with a foreach loop and bind values to your dropdown
foreach (item in queryResults)
{
ddlDate.items.add(item);
}
You can bind dropdown using linq by querying datatable (dtData) you have populated
ddlDate.DataValueField = "date_value";
ddlDepartment.DataTextField = "date_value";
ddlDepartment.DataSource = (from row in dtData.AsEnumerable()
select new { date_value = row["date_Column"].ToString()}).Distinct().ToList();
ddlDate.DataBind();
You can use TimeSpan in C# using that you can get difference between these days. like when you substract one date from another using Date.Substract(AnotherDate) method it will return you Timespan. and you can get number of days between two days like this example:
DateTime StartDate = DateTime.Now;
DateTime EndDate= DateTime.Now.AddMonths(5);
TimeSpan DateDiff = EndDate.Subtract(StartDate);
int Days = DateDiff.Days;
string Date;
for (int i = 1; i <= Days; i++)
{
Date = StartDate.AddDays(double.Parse(i));
}
Try this.
Related
I am trying to bind data to checkboxlist and am getting the below Error:
Data source is an invalid type. It must be either an IListSource,
IEnumerable, or IDataSource.
The Code i am using is:
for(var day = fromdate; day.Date <= todate; day = day.AddDays(1))
{
LeaveDtl.DataSource = day;
LeaveDtl.DataBind ();
}
If you want to add individual items in a loop, do it like this:
for (var day = fromdate; day.Date <= todate; day = day.AddDays(1))
{
LeaveDtl.Items.Insert(LeaveDtl.Items.Count, new ListItem(day.ToShortDateString(), day.ToShortDateString()));
}
How to get End Date after selection of Start date from drop down list.
I am selecting startdate from dropdowns and I am showing last date in label.
For example- If I am selecting "January" from first dropdown. Date "1" from second dropdown.
Then Label1.text become last date i.e. 31 december.
How can I do this ?
Please try below code
int month = DateTime.ParseExact(Convert.ToString(ddlMonth.SelectedValue), "MMMM", CultureInfo.CurrentCulture).Month;
int day = Convert.ToInt32(ddlDay.SelectedValue);
int year=DateTime.Now.Year;
DateTime date = new DateTime(year,month, day);
//Use AddDays for add and substract days
date.AddDays(-1);
string str=String.Format("{0:m}", date);
There are many ways of doing it . You can do it in javascript as well as in asp.net. have a page method of make a $.ajax call with the data selected
$.ajax({
url : '',
data : 'month=MONTH&day=DAY',
success : function(result){
$("#labelid").text(result);
}
})
C# part
int maxDay = DateTime.DaysInMonth(DateTime.Now.year,month);
//validate the selected day is equal or less than the maxDay
DateTime StartDate = new DateTime(DateTime.Now.Year, Convert.ToInt16(dropdownMonth.SelectedIndex) + 1, Convert.ToInt16(dropdownDays.SelectedIndex) + 1);
DateTime PreDayDate = StartDate.AddDays(-1); lblEndDateValue.Text = PreDayDate.ToString();
In case you do not want to do AJAX you have to do the postback and handle it then onward.
using
DateTime.AddDays Method
you can do this
DateTime StartDate = new DateTime(DateTime.Now.Year, Convert.ToInt16(UrMonthNameDropDown.SelectedIndex+1), Convert.ToInt16(UrDateDropdown.SelectedIndex));
DateTime PreDayDate = StartDate.AddDays(-1);
substract 1 day from your start date.
source:http://msdn.microsoft.com/en-IN/library/system.datetime.adddays.aspx
I have a date stored in a session variable, I then convert it to a date. Now I want to get the first and last date of the month for the date that is passed.
DateTime tempDate = System.Convert.ToDateTime(Session["Diary_Date"]);
DateTime startOfMonth = //what goes here?
DateTime endOfMonth = //??
One way to do this:
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
Another way is
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = new DateTime(tempDate.Year, tempDate.Month, DateTime.DaysInMonth(tempDate.Year, tempDate.Month));
I'm doing the following query to check if the current month is the same as the SQL field "Start".
If Today.Month = CDate(rsData("Start")).Month Then
What I'd like to do is switch it so that it will check within a 30 day period rather than identify the current month? Any ideas on how to do this?
If Date.Today.AddDays(-30) >= CDate(rsData("Start"))
' start date not older than 30 days '
End If
or if you have a variable date:
var minBoundary = New Date(2011,1,1)
var maxBoundary = New Date(2012,1,1)
var startDate = CDate(rsData("Start"))
If startDate >= MinBoundary AndAlso startDate <= maxBoundary
' start date between two dates '
End If
I believe in this case you would want to use the AddDays method of DateTime.
Dim mydate as DateTime = CDate(rsData("Start"))
Dim checkdate as DateTime = mydate.AddDays(30)
I am using an asp:Calander and I have an object that has a beginning date and an ending date. I need to get all the dates between these two dates and place them in an array so i can then render corresponding dates on the calander with different CSS
DateTime startDate;
DateTime endDate;
DateTime currentDate = startDate;
List<DateTime> dates = new List<DateTime> ();
while (true)
{
dates.Add (currentDate);
if (currentDate.Equals (endDate)) break;
currentDate = currentDate.AddDays (1);
}
It assumes that startDate < than endDate, you get the results on the "dates" list
IEnumerable<DateTime> RangeDays(DateTime RangeStart, DateTime RangeEnd) {
DateTime EndDate = RangeEnd.Date;
for (DateTime WorkDate = RangeStart.Date; WorkDate <= EndDate; WorkDate = WorkDate.AddDays(1)) {
yield return WorkDate;
}
yield break;
}
Untested code... but should work.
I voted up AlbertEin because he gave a good answer, but do you really need a collection to hold all the dates? When you are rendering the day, couldn't you just check if the date is withing the specified range, and then render it differently, no need for a collection. Here's some code to demonstrate
DateTime RangeStartDate,RangeEndDate; //Init as necessary
DateTime CalendarStartDate,CalendarEndDate; //Init as necessary
DateTime CurrentDate = CalendarStartDate;
String CSSClass;
while (CurrentDate != CalendarEndDate)
{
if(CurrentDate >= RangeStartDate && CurrentDate <= RangeEndDate)
{
CSSClass= "InRange";
}
else
{
CSSClass = "OutOfRange";
}
//Code For rendering calendar goes here
currentDate = currentDate.AddDays (1);
}
// inclusive
var allDates = Enumerable.Range(0, (endDate - startDate).Days + 1).Select(i => startDate.AddDays(i));
// exclusive
var allDates = Enumerable.Range(1, (endDate - startDate).Days).Select(i => startDate.AddDays(i));