pass in date and get first and last date of that month? - asp.net

I have a date stored in a session variable, I then convert it to a date. Now I want to get the first and last date of the month for the date that is passed.
DateTime tempDate = System.Convert.ToDateTime(Session["Diary_Date"]);
DateTime startOfMonth = //what goes here?
DateTime endOfMonth = //??

One way to do this:
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);

Another way is
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = new DateTime(tempDate.Year, tempDate.Month, DateTime.DaysInMonth(tempDate.Year, tempDate.Month));

Related

how to convert any string formatted date to datetime variable.?

domaindate.Text="31-03-3015";
DateTime dt = DateTime.Parse(domaindate.Text);
int day = dt.Day;
int month = dt.Month;
int year = dt.Year;
if (ddlyear.SelectedItem.Text == "1")
{
year = year + 1;
month = month - 1;
edate = String.Join("/", day, month, year);
}
p.expirydate = Convert.ToDateTime(edate);
Where p.expiredate id DateTime Property variable.
Geting Error:String was not recognized as a valid DateTime.
So, How i convert it to dd/MM/yyyy.?
Use the ParseExcact Method with the string format.
p.expirydate =DateTime.ParseExact(edate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
EDIT:
If you want add days to a specific date use the AddDays() Method
DateTime dt= DateTime.Parse(domaindate.Text);
p.expirydate = today.AddDays(number of days you want);

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

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.

Difference in years,months and days between 2 hijri dates

Please can anyone provide a method to calculate the difference between 2 hijri dates thanks in advance
i tried this code
HijriCalendar hijriCal=new HijriCalendar();
DateTimeFormatInfo DTFormat = new System.Globalization.CultureInfo("ar-sa", false).DateTimeFormat;
DTFormat.Calendar = new System.Globalization.HijriCalendar();
DTFormat.ShortDatePattern = "dd/MM/yyyy";
string HijriDate = FromDate.Date.ToString("d", DTFormat);
string[] fromDateParams=HijriDate.Split('/');
HijriDate = ToDate.Date.ToString("d", DTFormat);
string[] toDateParams = HijriDate.Split('/');
DateTime fromDateHijri = new DateTime(hijriCal.GetYear(FromDate), hijriCal.GetMonth(FromDate), int.Parse(fromDateParams[0]), hijriCal);
DateTime toDateHijri = new DateTime(hijriCal.GetYear(ToDate), hijriCal.GetMonth(ToDate), int.Parse(toDateParams[0]), hijriCal);
TimeSpan ts = ToDate.Subtract(FromDate);
As long as you just store the dates in normal datetimes, just treat them as any other date.
public TimeSpan GetDifference(this DateTime date1, DateTime date2) {
if (date1 < date2) {
return date2 - date1;
}
else if (date1 > date2) {
return date1 - date2;
}
return new TimeSpan(0);
}

Converting date to day of week

is there any ready to go solution within the microsoft framework, regarding conversion of date to day?
For example, i would like to convert this string 21/03/2010 (dd/mm/yyyy) to Sunday
Dim d = DateTime.Parse("21/03/2010").DayOfWeek()
This code will print Sunday on the console window
Dim dateToShow as DateTime = new DateTime(2010, 03,21)
Console.WriteLine(dateToShow.DayOfWeek.ToString)
This should print "Sunday".
string myDateTimeString = "21/03/2010";
DateTime dt = DateTime.ParseExact(
myDateTimeString, "dd/MM/yyyy",
new CultureInfo("en-Us", true)
, DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine(dt.DayOfWeek);
I would use DateTime.TryParse() just to validate the user input.
Dim input As String = "2010/12/23"
Dim dateTime As DateTime
If DateTime.TryParse(input, dateTime) Then
Console.WriteLine(dateTime.DayOfWeek)
Else
Console.WriteLine("Invalid")
End If

Resources