I'm creating a datetimepicker and I want it to have a maxDate until Saturday every week.
So for example, today is Sunday 16/10/2016, the maximum for this is 22/10/2016. But if i put maxdate = today + 7 , tuesday maxdate will be monday which I don't want.
I want to allow the user to be able to choose in 1 week only.
Is this possible ?
Edit:
Sorry,I wrote the wrong thing. It is calendar, not datetimepicker.
DateTime has a DayOfWeek property (that will output the DayOfWeek Monday if it is Monday). You could pass that property into a helper method that determines the number of days until Saturday based on the current day...
You could try this:
DateTime today = DateTime.Today;
int daysToAdd = 6 - (int)today.DayOfWeek;
DateTime nextSaturday = today.AddDays(daysToAdd);
Console.WriteLine(nextSaturday.ToShortDateString());
Ouputs:
10/22/2016
Related
I am trying to add a month to a date and populate textboxes based on the previous month. For months that end on the 31st (and even February 28th) this works. But, if the previous month ended on the 30th and the next month ends on the 31st, it is either one day short or one day long. For example:
Previous start date: 4/1/2017
Previous end date: 4/30/2017
New start date: 5/1/2017
New end date: SHOULD BE 5/31/2017
Here is the code I have:
// Set the Start Date
DateTime dtNewStartDate;
dtNewStartDate = Convert.ToDateTime(txtRecentBeginDate.Text).AddMonths(1);
txtNewStartDate.Text = dtNewStartDate.ToString();
// Set the End Date
DateTime dtNewEndDate;
dtNewEndDate = Convert.ToDateTime(txtNewStartDate.Text).AddMonths(1);
txtNewEndDate.Text = dtNewEndDate.ToString();
This produces an end date of 6/1/2017 instead of 5/31/2017
EDIT: I was able to find what I was looking for from https://social.msdn.microsoft.com/Forums/en-US/218260ec-b610-4fa6-9d1b-f56f3438b721/how-to-get-the-last-day-of-a-particular-month?forum=Vsexpressvcs.
This solution accounts for leap years and getting the correct last day of the month for any circumstance. This is what I came up with:
// Set the End Date
int intYear = dtNewStartDate.Year;
int intMonth = dtNewStartDate.Month;
int intNumberOfDays = DateTime.DaysInMonth(intYear, intMonth);
DateTime dtNewEndDate = new DateTime(intYear, intMonth, intNumberOfDays);
txtNewEndDate.Text = dtNewEndDate.ToString();
You can just get the last day of the month for that month.
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));
This is what I typically do: Just take the year and month from the start date, create a new date based on that, add a month and subtract a day which ends up being the last day of the next month::
DateTime dtNewEndDate = new DateTime(dtNewStartDate.Year, dtNewStartDate.Month, 1)
.AddMonths(1)
.AddDays(-1);
I have a jquery calender that a user selects a date from.
I also have a list from Mon - Sun that the user can click on that would be the Monday through Sunday for the selected week from the calendar.
I can get the current day of the week using c#'s DayOfWeek method from jquery's selected date.
I have the user selected day of the week from the mon-sun list (1-7)
but how do i form a new date based of what day the user clicks on from the mon - sun list?
an example would be user selects Friday the 16th of Jan 2015 from Jquery Calendar.
They then click on the Monday from the Select list.
I should be able to calculate the Monday is the 12th of Jan 2015
cheers
Here's you go,
DateTime selectedDate = DateTime.Now; //your selected date
DayOfWeek s = selectedDate.DayOfWeek; // your selected day of the selecteddate.
DayOfWeek selectedWeek = DayOfWeek.Friday; //your selected weekday.
DateTime output = selectedDate.AddDays((int)selectedWeek - (int)s); //output.
DateTime dateTime = DateTime.Now;
var date = dateTime.DayOfWeek.ToString();
and with
DateTime dateTime = new DateTime(2015, 1, 12);
you can get the DayOfWeek from a specific Day.
DayOfWeek is an enum. So when you have Monday it may have e.g. integer 1 behind. When you have Friday now you have e.g. integer 5 behind.
I think now you can calculate that Friday is 4 days later than Monday. so it is the DateTime(2015, 1, 12 + 4) for the Friday
Does this help?
user just enter the day of week. For instance user enter friday. I need to find the exact date of given day and format will be like dd.MM.yyyy.
But I don't know how I do it.
Example:
label1 - Friday (entered by user)
label2 - 08.06.2012 (found by system)
label1 is just a string (just Friday). It's coming from webservice as a string variable. I need to find the date and compare with today, If it's not equal or small than today I give date of upcoming Friday, else I give the date of the Friday the week after.
"If it's not equal or small than today I give exact date, else I give next week date. "
Assuming that means that you return always the next date in future with the given day of week, the only exception is when today is the given day of week.
public static DateTime getNextWeekDaysDate(String englWeekDate)
{
var desired = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), englWeekDate);
var current = DateTime.Today.DayOfWeek;
int c = (int)current;
int d = (int)desired;
int n = (7 - c + d);
return DateTime.Today.AddDays((n >= 7) ? n % 7 : n);
}
Let's test:
DateTime Monday = getNextWeekDaysDate("Monday"); // 2012-06-11
DateTime Tuesday = getNextWeekDaysDate("Tuesday"); // 2012-06-05 <-- !!! today
DateTime Wednesday= getNextWeekDaysDate("Wednesday"); // 2012-06-06
DateTime Thursday = getNextWeekDaysDate("Thursday"); // 2012-06-07
DateTime Friday = getNextWeekDaysDate("Friday"); // 2012-06-08
Create enum of days (i.e. monday - 0, tuesday - 1, etc);
Get DateTime.Now DayOfWeek and cast in some way it to your enum value.
Calculate difference between Now.DayOfWeek and user's day of the week;
Use DateTime.AddDays(difference).DayofTheWeek;
get current time with DateTime.now
Current day is DateTime.Now.DayOfWeek
Then get the day of week your user entered
Then your result is DateTime.now.AddDays( NowDayOfWeek - UserDayOfWeek).
We have a unique requirment where we have our own start day of a week and end day of a week(C#),
Contrary to the existing Date Time class whose start day is Sunday and end day is Saturday, we have Wednesday as our start day and Tuesday our EndDay (and this will vary per client).
and we need to implement the following logic
a) for a given date get the start Date of a week and end Date of a week
Ex: based on the above if the current day is 23/Sep/2010 (Thur) we need to get 22/Sep/2010 (Wed) as our Start Day and 28/Sep/2010 (Tue) as our end day
i apologize there is a correction the end day should be Tuesday (7 days a week)
Can anyone help here?
Thanks
Couldn't you just do
(dt.DayOfWeek + delta) % 7
?
Here is my inelegant crack at it:
public static void CalculateWeek (out DateTime WeekStart, out DateTime WeekEnd, DateTime InputDate)
{
DateTime tempDT = InputDate;
while (tempDT.DayOfWeek != DayOfWeek.Wednesday)
{
tempDT = tempDT.AddDays(-1);
}
WeekStart = tempDT.Date;
while (tempDT.DayOfWeek != DayOfWeek.Tuesday)
{
tempDT = tempDT.AddDays(1);
}
WeekEnd = tempDT.Date;
}
By no means clever or superefficient, although the loops will iterate no more than a trivial 7 times.
using c# visual studio 2008.
Can anyone help with an algorithm to do this please
if i have a range of days selected for this week (eg monday to friday) i can find the dates for these using the datetime functions available.
What i want to do is compared to stored data for the same DAY range 1 year ago.
So basicly i need to go back 1 year and find the dates for the nearest Mon to fri DAY range from 1 year previous. I guess i also need to take into acount leap years.
Can anyone help with a suitable algorithm on how to achieve this.
Of course the DAY for todays date last year is not going to be the same day.
thanks in advance
Here's some code which might do what you want - but the test cases show that there are corner cases to consider:
using System;
public class Test
{
static void Main()
{
Console.WriteLine(SameDayLastYear(DateTime.Today));
Console.WriteLine(SameDayLastYear(new DateTime(2010, 12, 31)));
}
static DateTime SameDayLastYear(DateTime original)
{
DateTime sameDate = original.AddYears(-1);
int daysDiff = original.DayOfWeek - sameDate.DayOfWeek;
return sameDate.AddDays(daysDiff);
}
}
What would you want the result for the second call to be? This code returns January 1st 2010, because that's the closest date to "a year ago on the same day".
I strongly suggest that whatever you go with, you have unit tests checking leap years, start and end of year etc.
Let's say you select Wednesday 10-02-2010 - Friday 12-02-2010 this year.
Last year that would have been Tuesday 10-02-2009 - Thursday 12-02-2009.
So you can do the following: Go back a year by simply performing DateTime.AddYears(-1). Make sure you correct for leap years here.
Then you use .AddDays(1) until you end up on a Wednesday - Friday timeframe.
That way you only have to take leap years into account at one point and this should produce the result you need.
I just subtracted one year then ran backwards until I found a Monday. LastYear will end up being the first Monday before this date last year
DateTime LastYear = DateTime.Now.AddYears(-1)
DayOfWeek Check = LastYear.DayOfWeek;
while (Check != DayOfWeek.Monday)
{
LastYear = LastYear.addDays(-1);
Check = LastYear.DayOfWeek;
}
Console.WriteLine("{0}",LastYear);
DateTime now = DateTime.Now;
DateTime lastyear = now.AddYears(-1);
string dayOfWeek = lastyear.DayOfWeek.ToString();
if (dayOfWeek.Equals("Saturday")) { dayOfWeek = "Friday"; }
else if (dayOfWeek.Equals("Sunday")) { dayOfWeek = "Monday"; }
Console.WriteLine(dayOfWeek);
Console.ReadKey();
Get a datetime object for last year, then use the DayOfWeek property.
This was pretty fun.
// today's info
DateTime today = DateTime.Now;
DayOfWeek today_name = today.DayOfWeek;
// this day one year ago
DateTime year_ago = today - new TimeSpan( ((today.Year - 1) % 4) ? 365 : 366, 0, 0, 0);
// find the closest day to today's info's name
DayOfWeek today_name_a_year_ago = year_ago.DayOfWeek;
DateTime current_range_a_year_ago = year_ago - new TimeSpan( year_ago.DayOfWeek - today_name, 0, 0, 0);
Console.WriteLine( "Today is {0}, {1}", today_name, today);
Console.WriteLine( "One year from today was {0}, {1}", today_name_a_year_ago, year_ago);
Console.WriteLine( "New date range is {0}", current_range_a_year_ago);
I would highly recommend using the unit testing features built into VS2008 to make sure you account for corner cases.