How to populate weeks of a month using asp.net? - 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

Related

moment.js black friday this month boolean

I want to make a js function. When I input year and month number, if there is a black friday in that month, it will returns true.
Is there any simlar moment.js method or function to do this? Thx.
You can create a moment object representing the 13th of the given month using using moment({unit: value, ...}); then you can get day of week using day(). As docs says:
This method can be used to set the day of the week, with Sunday as 0 and Saturday as 6.
so you can check if day() is equal to 5 (Friday).
Here a working example:
function hasBlackFriday(year, month) {
return (moment({y: year, M: month, d: 13}).day() === 5);
}
// Test foreach month of 2017
for(var i=0; i<12; i++){
console.log(hasBlackFriday(2017, i));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Keep in mind that, as moment({unit: value, ...}); docs says:
Note that like moment(Array) and new Date(year, month, date), months are 0 indexed.
As Matt Johnson highlighted in the comments, you can get the same result also using native JavaScript Date object, here a sample:
function hasBlackFriday(year, month) {
return (new Date(year, month, 13).getDay() === 5);
}
// Test foreach month of 2017
for(var i=0; i<12; i++){
console.log(hasBlackFriday(2017, i));
}
This function returns the date for last Friday of the given month m and year y.
function hasBlackFriday(y, m) {
var date = moment(new Date(y, m - 1, '1'))
result = date.endOf('month');
while (result.day() !== 5) {
result.subtract(1, 'day');
}
return result;
}
console.log(hasBlackFriday(2017, 4).format('M DD YY'))

Get week of month with Joda-Time

Is it possible to parse a date and extract the week of month using Joda-Time. I know it is possible to do it for the week of year but I cannot find how/if it is possible to extract the week of month.
Example: 2014-06_03 where 03 is the third week of this month
DateTime dt = new DateTime();
String yearMonthWeekOfMonth = dt.toString("<PATTERN for the week of month>");
I have tried the pattern "yyyyMMW" but it is not accepted.
Current joda-time version doesn't support week of month, so you should use some workaround.
1) For example, you can use next method:
static DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MM_'%d'");
static String printDate(DateTime date)
{
final String baseFormat = FORMATTER.print(date); // 2014-06_%d
final int weekOfMonth = date.getDayOfMonth() % 7;
return String.format(baseFormat, weekOfMonth);
}
Usage:
DateTime dt = new DateTime();
String dateAsString = printDate(dt);
2) You can use Java 8, because Java's API supports week of month field.
java.time.LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM_W");
System.out.println(formatter.format(date));
This option in Joda is probably nicer:
Weeks.weeksBetween(date, date.withDayOfMonth(1)).getWeeks() + 1
For the case you don't like calculations so much:
DateTime date = new DateTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date.toDate());
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
If the start day of week is Monday then you can use it:
public int getWeekOfMonth(DateTime date){
DateTime.Property dayOfWeeks = date.dayOfWeek();
return (int) (Math.ceil((date.dayOfMonth().get() - dayOfWeeks.get()) / 7.0)) + 1;
}

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

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