Finding which day of month? - asp.net

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

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 get date range for a particular week number

I am trying to get date range for a particular week number.
Following is my code to get week number with respect to current date
final date = DateTime.now();
final startOfYear = new DateTime(date.year, 1, 1, 0, 0);
final firstMonday = startOfYear.weekday;
final daysInFirstWeek = 8 - firstMonday;
final diff = date.difference(startOfYear);
var weeks = ((diff.inDays - daysInFirstWeek) / 7).ceil();
if (daysInFirstWeek > 3) {
weeks += 1;
}
print("Week Range $weeks");
What I don't understand is how I get start and end date for a particular week number.
Any help would be appreciated.
Assuming your week is zero based, you need to do something like this:
final date = new DateTime.now();
final startOfYear = new DateTime(date.year, 1, 1, 0, 0);
final firstMonday = startOfYear.weekday;
final daysInFirstWeek = 8 - firstMonday;
final diff = date.difference(startOfYear);
var weeks = ((diff.inDays - daysInFirstWeek) / 7).ceil();
main(){
int week = weeks;
print("Start Date for week $week: ${startOfYear.add(Duration(days: 7*week))}");
print("End Date for week $week: ${startOfYear.add(Duration(days: 7*week+6))}");
}
To get the starting and day of week from week number and year.
DateTime getDateByWeekNumber({
int weeknumber,
int year,
bool start
}) {
//check if start == true retrun start date of week
//else return end date
var days = ((weeknumber - 1) * 7) + (start ? 0 : 6);
return DateTime.utc(year, 1, days);
}
This is in accordance to ISO 8601. You can very week number here

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

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

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