How to get end date - asp.net

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

Related

Need first day of last month and Last day of next month from current date in Asp.Net

Need first day of last month and Last day of next month from current date in Asp.Net
Eg : Current date 02/12/2019
first day of last month = 01/11/2019
Last day of next month = 31/01/2020
var myDate = DateTime.Now;
var firstDateOfLastMonth = myDate.AddMonths(-1);
firstDateOfLastMonth = firstDateOfLastMonth.AddDays(-firstDateOfLastMonth.Day + 1);
var lastDayOfNextMonth = myDate.AddMonths(1);
lastDayOfNextMonth = new DateTime(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month, DateTime.DaysInMonth(lastDayOfNextMonth.Year, lastDayOfNextMonth.Month));
You may have to refer DataTime C# for more info
please try below line of code:
DateTime lastMonthdate = DateTime.Now.AddMonths(-1);
var lastMonth = new DateTime(lastMonthdate.Year, lastMonthdate.Month, 1);
var lastDayOfNextMonth = lastMonth.AddYears(1).AddMonths(1).AddDays(-1);

How to refresh displayed date in CalendarExtender?

I've this code that changes the maximum date of a second Calendar Extender to 90 days after the date that has been defined in a first one and minimum to the same it has been selected on the first and it works right except for one thing.
var cal2 = $find("calendar2");
var fecha = cal._selectedDate;
var date = fecha.getDate() + 90;
var year = fecha.getFullYear();
var month = fecha.getMonth();
var todayDate = new Date(year, month, date);
cal2._startDate = cal._selectedDate;
cal2._selectedDate = fecha;
cal2._switchMonth(fecha);
cal2._endDate = todayDate;
Problem is that if I first seelct a date on cal, dates are properly shown in cal2, but I select one on cal again then cal2 doesn't display in the same month that cal, what's much worse it displays to select days that would be now impossible to select and in fact you can select them unless you go back first to month mode.
Any idea on how to "refresh" the behavior of the second CalendarExtender?
Thank you.
you can use this method.
Sys.Extended.UI.CalendarBehavior.prototype.refresh = function () {
this._isOpen = true;
this._ensureCalendar();
this.invalidate();
this._isOpen = false;
}
When you add the method only call:
cal2.refresh()

DATE BIND IN DROP DOWN LIST

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.

VB.net - CDate with SQL

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)

DateTime [ Last week , Last month periods ]

I need to setup last week , last month periods on changing dropdownlist
I'm making
switch (DDL.SelectedIndex)
{
case 0:
{
// last week
this.TextBox3.Text = DateTime. //Previos week first day
this.TextBox4.Text = DateTime. //Previos week last day
} break;
case 1:
{
// last mouth
this.TextBox3.Text = DateTime.// Previos month first day
this.TextBox4.Text = DateTime.// Previos month last day
} break;
}
So is there some ways how can I select date values like I want ?
also , I've got AJAX calendar extender on text boxes
thank you.
Something like this, I think:
int dayOfWeekNumber = (int)DateTime.Today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
var previosWeekFirstDay = DateTime.Today.AddDays(-7 - dayOfWeekNumber);
var previosWeekLastDay = previosWeekFirstDay.AddDays(6);
var previosMonthFirstDay = DateTime.Today.AddMonths(-1);
previosMonthFirstDay = previosMonthFirstDay.AddDays(-previosMonthFirstDay.Day + 1);
var previosMonthLastDay = previosMonthFirstDay.AddDays(DateTime.DaysInMonth(previosMonthFirstDay.Year, previosMonthFirstDay.Month) - 1);
Edited: see Fredrik Mörk comment.

Resources