To calculate days in a month - asp.net

i have a textbox that displays date in format,for eg:March,20,2008.Then i need to get total days n march in year 2008.Can anybody help

You can use the DaysInMonth function
e.g.
Date.DaysInMonth(2008, 3)
Obviously, you'll have to pass the year and month to the function

int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int numDays = DateTime.DaysInMonth(year, month);

Use DataTime.DaysInMonth()
http://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth(VS.71).aspx

Remember to use the date they entered.
VB Code:
dim enteredDate as DateTime = cdate(txtDateBox.text)
dim daysInMonth as Int = DateTime.DaysInMonth(enteredDate.year,enteredDate.month)

Related

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

Get a first and last day of the month from month key

I have a month key which look like this "201208".
I am reading the data from SQL and returning as a DataTable.
Then I am looping through my DataTable.
For Each dr As DataRow In dt.Rows
Dim s As String = dr.Item(0)
Next
My s would be a string looking like this "201208"
How convert this string into year, first day of the month and last day of the month?
Looking for a way to split the string into two seperate dims.
Any suggestions much appreciated.
The easiest way is to just parse the string as Date, then calculate the last day of the month.
Try the following:
Dim s = "201208"
Dim firstDay = Date.ParseExact(s, "yyyyMM", Nothing)
Dim lastDay = firstDay.AddMonths(1).AddDays(-1)
Dim year = firstDay.Year
Firstly I would use DateTime.TryParseExact Method with format "ddMMyy" to parse the string into a DateTime.
Then I would use something like
Dim startDate As New DateTime(dbDate.Year, dbDate.Month, 1)
Dim endDate As DateTime = startDate.AddMonths(1).AddDays(-1)
to get the StartDate and EndDate
This works for me:
Function FormatDate(ByVal dateKey As String) As String
Dim x As DateTime = DateTime.ParseExact(dateKey, "yyyyMM", Nothing)
FormatDate = String.Format("Year: {0}; First day of month: {1}; Last day of month: {2}", x.Year, x.AddDays(-x.Day).ToString("dddd"), x.AddMonths(1).AddDays(-x.Day).ToString("dddd"))
End Function
You can use this function as so:
Dim dateDetails As String = FormatDate("201208")
Split that string into a year and month (see Substring)
Use "1" as the first of the month -> that's your start date
Add one month, subtract one day -> that's your end date

int to string to date

i use asp.net
i have a int (10112009)
in the end i want a date formate like day- day / month month / yyyy
what's the best way (or a way) to do that?
thanks
I would do something like this:
int n = 10112009;
DateTime date;
if (DateTime.TryParseExact(n.ToString("00000000"), "ddMMyyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
// use date
}

Resources