DateTime [ Last week , Last month periods ] - asp.net

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.

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()

How to get end date

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

Finding which day of month?

I am using Asp:calendar. I need to find the day of week of the date selected.
For example for the month of February,2014 if 27th is selected it should give value as 4th Thursday. Please help in this
You can get the Day Of Week through DateTime.DayOfWeek property. like:
DayOfWeek daOfWeek = youCalendar.SelectedDate.DayOfWeek;
To calculate the occurrence of the day in that particular month, Create a new DateTime starting from 1st of the month, Loop through to SelectedDate check for DayOfWeek equals to SelectedDate.DayOfWeek and increment a counter.
DateTime selecteDate = yourCalendar.SelectedDate;
DateTime startDate = new DateTime(selecteDate.Year, selecteDate.Month, 1);
int counter = 0;
while (startDate <= selecteDate)
{
if (startDate.DayOfWeek == selecteDate.DayOfWeek)
counter++;
startDate = startDate.AddDays(1);
}

How to populate weeks of a month using asp.net?

I want to populate the weeks in a dropdown list when selecting month like
when I select
January
Then it will show only the weeks which starts from monday like
1st week:
Monday(02/01/2012)
2nd week:
Monday(09/01/2012)
3rd week:
Monday(16/01/2012)
4th week:
Monday(23/01/2012)
5th week:
Monday(30/01/2012)
So it sounds like you need to find the first Monday in the week, then just keep adding one week until you're not in the same month any more:
using System;
using System.Collections.Generic;
class Test
{
static void Main(string[] args)
{
foreach (DateTime date in GetMondays(2012, 1))
{
Console.WriteLine(date);
}
}
static IEnumerable<DateTime> GetMondays(int year, int month)
{
DateTime startOfMonth = new DateTime(year, month, 1);
// Get to the first Monday
int daysToMonday = DayOfWeek.Monday - startOfMonth.DayOfWeek;
// Now make sure it's non-negative...
int daysToNextMonday = (daysToMonday + 7) % 7;
// Add it to the start of the month to get to the first Monday
DateTime firstMonday = startOfMonth.AddDays(daysToNextMonday);
// Now yield and iterate until we're done
for (DateTime date = firstMonday;
date.Month == month;
date = date.AddDays(7))
{
yield return date;
}
}
}
(You could add the dates to a list and return that instead if you wanted... it doesn't make much difference really.)
simply parse everything from the starting date of the month to the ending date of the month.
checking DayOfWeek for every day and print out the results

Resources