.NET gives me the current time in both local and UTC time zones (or any other) in the DateTime struct.
Given only hour/minute variables, find when the next occurrence of this time period (eg 6:30 PM/AM) will occur, with the ability to retrieve more future times at will.
It sounds easy but indeed friends this has been breaking my noodle for a good while.
Edit:
Example:
~-------|now|------??-----------|future known time|------------~
~-------2pm------??2-----------9am------------~
??2 = 19
If I understand correctly you want to know how much time has to pass in order to hit the next given hour:minute. You can use the TimeSpan struct for this.
//this is your target time from 1 to 12 h
var future = new TimeSpan(11, 30, 0);
//TimeOfDay gives you the time elapsed since midnight as a TimeSpan
var difference = future.Subtract(DateTime.Now.TimeOfDay);
//check for negative TimeSpan,
//it means the target time occurs on the next day, just add 24 hours
if (difference < TimeSpan.Zero)
difference = difference.Add(TimeSpan.FromDays(1));
Now you have a TimeSpan that represents what you need. You can use its properties to express it as you seem fit. For example:
difference.TotalHours; //(double) total time as a fractional hour
difference.Hours; //(int) just the hour component of the total time
As for retreiving more future times (am and pm), you can just add 12 more hours to difference to get the next occurence.
This was cooded in stackoverflow form, so it's probably there are some typos. Either way, you'll get the big picture.
public DateTime search(int hour, int min) {
if (hour >= 12)
return partialSearch(hour - 12, hour, min);
else
return partialSearch(hour, hour + 12, min);
}
public DateTime partialSearch(int morningHour, int afternoonHour, int min) {
DateTime now = DateTime.now;
if (now.hour == morningHour || now.hour == afternoonHour) {
if (now.minutes <= min) {
return now.AddMinutes(min - now.minutes);
}
now = now.AddHour(1);
}
now = now.AddMinutes(-now.Minutes); // set the minutes to 0
while(now.hour != morningHour || now.hour != afternoonHour) {
now = now.AddHour(1);
}
return now.addMinutes(min);
}
Not sure if I fully understood your requirements, but it seems you are looking for something like this?
public class TimeIterator
{
public DateTime CurrDateTime { get; set; }
public IEnumerable<DateTime> GetTimes(short count)
{
for (short i = 1; i <= count; i++)
yield return this.CurrDateTime.AddHours(i * 12);
}
public TimeIterator()
{
this.CurrDateTime = DateTime.Now;
}
}
This code can be easily adjusted to work with any time interval - not just 12 hour interval
Related
This question already has answers here:
Timer callback raised every 24 hours - is DST handled correctly?
(3 answers)
Closed 5 years ago.
I'm not in front of my code right now, but don't really think I need it to ask this question. So I have a countdown timer that goes off every 18 hours and then resets. The timer checks the current DateTime.Now and adjusts the countdown timer as needed. I am having an issue trying to account for when daylight savings when it goes back an hour because; for example this past event on November 5th 2017 at 2am it goes back to 1am but when I do DateTime.IsDaylightSavingTime() it tells me that it's false even though Daylight Saving Time just went off. This makes my timer go back an extra hour because it thinks that Daylight Saving Time still hasn't happen for that one hour period. How would I get around this?
If you realy need to use local time for some reason, than you should count for DST changes in advance (prior scheduling next event).
TimeZoneInfo.GetAdjustmentRules() should help you to get the time and delta of next DST adjustment.
Following is the code for getting upcomming adjustment:
public static DateTime? GetNextAdjustmentDate(TimeZoneInfo timeZoneInfo)
{
var adjustments = timeZoneInfo.GetAdjustmentRules();
if (adjustments.Length == 0)
{
return null;
}
int year = DateTime.UtcNow.Year;
TimeZoneInfo.AdjustmentRule adjustment = null;
foreach (TimeZoneInfo.AdjustmentRule adjustment1 in adjustments)
{
// Determine if this adjustment rule covers year desired
if (adjustment1.DateStart.Year <= year && adjustment1.DateEnd.Year >= year)
adjustment = adjustment1;
}
if (adjustment == null)
return null;
//TimeZoneInfo.TransitionTime startTransition, endTransition;
DateTime dstStart = GetCurrentYearAdjustmentDate(adjustment.DaylightTransitionStart);
DateTime dstEnd = GetCurrentYearAdjustmentDate(adjustment.DaylightTransitionEnd);
if (dstStart >= DateTime.UtcNow.Date)
return dstStart;
if (dstEnd >= DateTime.UtcNow.Date)
return dstEnd;
return null;
}
private static DateTime GetCurrentYearAdjustmentDate(TimeZoneInfo.TransitionTime transitionTime)
{
int year = DateTime.UtcNow.Year;
if (transitionTime.IsFixedDateRule)
return new DateTime(year, transitionTime.Month, transitionTime.Day);
else
{
// For non-fixed date rules, get local calendar
System.Globalization.Calendar cal = CultureInfo.CurrentCulture.Calendar;
// Get first day of week for transition
// For example, the 3rd week starts no earlier than the 15th of the month
int startOfWeek = transitionTime.Week * 7 - 6;
// What day of the week does the month start on?
int firstDayOfWeek = (int)cal.GetDayOfWeek(new DateTime(year, transitionTime.Month, 1));
// Determine how much start date has to be adjusted
int transitionDay;
int changeDayOfWeek = (int)transitionTime.DayOfWeek;
if (firstDayOfWeek <= changeDayOfWeek)
transitionDay = startOfWeek + (changeDayOfWeek - firstDayOfWeek);
else
transitionDay = startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek);
// Adjust for months with no fifth week
if (transitionDay > cal.GetDaysInMonth(year, transitionTime.Month))
transitionDay -= 7;
return new DateTime(year, transitionTime.Month, transitionDay);
}
}
You'd need to add some more code to retrieve and apply the adjustment delta.
Well - now, when you see all the hard work that would need to be done (and than maintained and made sure to be bug free), you might want to rething your problem to be able to use UTC.
I would use DateTime.UtcNow you won't have the issue with daylight savings
I am developing c#.net solution where i have to calculate Start Date based on provided End date and Duration (back days) without weekends.
i.e. End Date: 05/5/2011
Back days: 5
Start Date = (05/5/2011) - 5 days (Excludes weekends)
Start Date = 29/04/2011
thanks you,
Something like this is probably what I'd do:
DateTime CalcStartDate(DateTime endTime, int daysBack)
{
DateTime startTime = endTime.Date;
while (daysBack > 0)
{
startTime = startTime.AddDays(-1);
if (startTime.DayOfWeek != DayOfWeek.Saturday && startTime.DayOfWeek != DayOfWeek.Sunday)
{
--daysBack;
}
}
return startTime;
}
Or even better, Bala's suggestion of using a library. Date and time is messy, a hardened/tested library is usually a good choice.
Bala R has the answer correctly. Here is a link to an article on how to use the AddBusinessDays() method:
http://www.codeproject.com/KB/cs/AddBusinessDay.aspx
I don't know how far back you are going to go. If it is a lot of days back then looping through the days might be a little CPU intensive. Well, probably not with modern processors...
I decided to implement a solution without a loop.
My code is a little more difficult to read, but it should be more efficient performance-wise.
public static class DateTimeExtensions
{
public static DateTime SubtractBusinessDays(this DateTime fromDateTime, int days)
{
var subtractDays = days % 5;
var dayNumber = fromDateTime.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)fromDateTime.DayOfWeek;
var addDays = Math.Max(dayNumber - 5, 0);
var result = fromDateTime.AddDays(addDays - subtractDays - (days / 5 * 7));
if ((addDays + dayNumber) % 7 <= subtractDays)
result = result.AddDays(-2);
return result;
}
}
I am new at C#. I'd like to check whether a time is between 2 given hours, and if so then do something. Can anyone give me an example?
pseudocode example:
int starthour = 17;
int endhour = 2;
if ( hour between starthour and endhour){
dosomething();
}
How do I write a check on whether hour is between starthour and endhour? In C#, the time is returned in AM/PM format so I don't know if it will understand the 17 number as "5 PM".
Assuming you're talking about the current time, I'd do something like this:
// Only take the current time once; otherwise you could get into a mess if it
// changes day between samples.
DateTime now = DateTime.Now;
DateTime today = now.Date;
DateTime start = today.AddHours(startHour);
DateTime end = today.AddHours(endHour);
// Cope with a start hour later than an end hour - we just
// want to invert the normal result.
bool invertResult = end < start;
// Now check for the current time within the time period
bool inRange = (start <= now && now <= end) ^ invertResult;
if (inRange)
{
DoSomething();
}
Adjust the <= in the final condition to suit whether you want the boundaries to be inclusive/exclusive.
If you're talking about whether a time specified from elsewhere is within a certain boundary, just change "now" for the other time.
Actually, if we're dealing with pure hours here like a Abelian Ring from 0 to 23 and 0 again, I believe the following is actually a working solution:
(start <= end && start <= t && t <= end) or (start > end && (start <= t || t <= end))
Complex though this is, it is essentially an if-else where you have a different algorithm depending on whether start <= end or not, where t is the time you wish to test. In the first case, start and end are normal order, so t must be both greater than start and less than end. In the case where start is greater than end, the times outside the opposite range are what we want:
NOT(end < t and t < start)
Using DeMorgan's theorem:
NOT(end < t) or NOT(t < start)
NOT(t < start) or NOT(end < t)
t >= start or end >= t
start <= t or t <= end
This should solve your and my problems.
#JonSkeet
The thing is, looking at your algorithm, let's assume for a moment the time is 1am, day 1.
Now holds 1am Day 1
Today holds midnight Day 1
Start holds 5pm Day 1 (given the original example)
End holds 2am Day 1 (again from the example)
End holds 2am Day 2 (since start > end)
Now, unless I'm mistaken, start ≰ now since start is 5pm Day 1 and now is 1am Day 1 which is before now, therefore the test fails but the original question wanted 1am included in the range since 1am is between 5pm and 2am. Did I miss something?
#Brian
Also, looking at your code, I think you can detect 1am but now you would have a problem with 10pm (22:00) since your times become:
Start is 17
End is 26
Now is 22 + 24 = 46! so you will fail in the less-than test.
Clearly, the general case is very tricky! More so when you're restricted to Google Spreadsheets as I am.
When subtracting DateTimes, you get a TimeSpan struct that you can query for things like the total number of hours (the TotalHours property):
TimeSpan ts = starttime - endtime;
if(ts.TotalHours > 2)
{
dosomething();
}
If you want to see if the times are identical, then you can use TotalMilliseconds - for identical DateTimes, this will be equal to 0.
If you want to compare minutes also like I do you can use this snippet of code in java.
//Initialize now, sleepStart, and sleepEnd Calendars
Calendar now = Calendar.getInstance();
Calendar sleepStart = Calendar.getInstance();
Calendar sleepEnd = Calendar.getInstance();
//Assign start and end calendars to user specified star and end times
long startSleep = settings.getLong("startTime", 0);
long endSleep = settings.getLong("endTime", 0);
sleepStart.setTimeInMillis(startSleep);
sleepEnd.setTimeInMillis(endSleep);
//Extract hours and minutes from times
int endHour = sleepEnd.get(Calendar.HOUR_OF_DAY);
int startHour = sleepStart.get(Calendar.HOUR_OF_DAY);
int nowHour = now.get(Calendar.HOUR_OF_DAY);
int endMinute = sleepEnd.get(Calendar.MINUTE);
int startMinute = sleepStart.get(Calendar.MINUTE);
int nowMinute = now.get(Calendar.MINUTE);
//get our times in all minutes
int endTime = (endHour * 60) + endMinute;
int startTime = (startHour * 60) + startMinute;
int nowTime = (nowHour * 60) + nowMinute;
/*****************What makes this 100% effective***************************/
//Test if end endtime is the next day
if(endTime < startTime){
if(nowTime > 0 && nowTime < endTime)
nowTime += 1440;
endTime += 1440;
}
/**************************************************************************/
//nowTime in range?
boolean inRange = (startTime <= nowTime && nowTime <= endTime);
//in range so calculate time from now until end
if(inRange){
int timeDifference = (endTime - nowTime);
now.setTimeInMillis(0);
now.add(Calendar.MINUTE, timeDifference);
sleepInterval = now.getTimeInMillis() / 1000;
editor.putBoolean("isSleeping", true);
editor.commit();
Log.i(TAG, "Sleep Mode Detected");
returned = true;
}
bool CheckHour(DateTime check, DateTime start, DateTime end)
{
if (check.TimeOfDay < start.TimeOfDay)
return false;
else if (check.TimeOfDay > end.TimeOfDay)
return false;
else
return true;
}
int starthour = 17;
int endhour = 2;
int nowhour = DateTime.Now.Hour;
if (endhour < starthour)
{
endhour+=24;
nowhour+=24;
}
if (starthour <= nowhour && nowhour <= endhour)
{
dosomething();
}
I'm not sure which I prefer between this code and Jon Skeet's code.
Using Jon Skeet's solution above I added a fix where if start time is after beginning time eg You start the job after 6pm at night and end it the next morning at 5am. then you need to check this and apply another day to the end time. Hope it helps, I personally have spent too much time on this piece of work. have a great day :)
if (stopHour < startHour)
{
end = today.AddHours(stopHour+24);
}
Full Code is below.
private static bool IsInRunHours()
{
try
{
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
// after = 18 before = 5
// Only take the current time once; otherwise you could get into a mess if it
// changes day between samples.
DateTime now = DateTime.Now;
DateTime today = now.Date;
Int32 startHour = ConfigurationManager.AppSettings["UpdateRunAfter"].ToInt();
Int32 stopHour = ConfigurationManager.AppSettings["UpdateRunBefore"].ToInt();
DateTime start = today.AddHours(startHour);
DateTime end = today.AddHours(stopHour);
if (stopHour < startHour)
{
end = today.AddHours(stopHour+24);
}
//ConfigurationManager.AppSettings["UpdateRunBefore"].ToInt()
//ConfigurationManager.AppSettings["UpdateRunAfter"].ToInt()
// Cope with a start hour later than an end hour - we just
// want to invert the normal result.
bool invertResult = end < start;
// Now check for the current time within the time period
bool inRange = (start <= now && now <= end) ^ invertResult;
if (inRange)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
Assume I've got a start and stop hour in which some processing should take place:
Start 20:00
End 07:00
Now what is the best algorithm to check if a certain DateTime hour value falls within this range? Thanks in advance!
Please note that the start and end times mentioned above indicate that we are dealing with an "overnight-job". Meaning that the period to be checked starts at 20:00 in the evening and ends at 07:00 on the following morning.
Assume you only have the time and not the date.
if end_time >= start_time:
return start_time <= current_time <= end_time
else:
return start_time <= current_time or current_time <= end_time
If you are sure it is in the same day
you also do not seem to care for seconds
Transform everything in minutes
startminute = 20 * 60 + 0
endminute = 7 * 60 + 0
eventminute = x * 60 + y // with event having the form xx:yy
return startminute < eventminute && eventminute < endminute
Another option would be to get the 3 times in DateTime format
DateTime start, end, event
return (start < event && event < end);
Assuming you have a start, end and now DateTime, you could use
bool within = start.TimeOfDay.TotalHours <= now.TimeOfDay.TotalHours &&
now.TimeOfDay.TotalHours <= end.TimeOfDay.TotalHours;
Without knowing how to do it in C#, I'd go with converting start and end time to a timestamp and then do a simple if (end time >= given time AND start time <= given time) comparison. Maybe that'll get you started or give you a hint what to search for.
I think c# supports greater than/less than operators for DateTime variables, so just say
if ((beginDateTime<myDateTime)&&(myDateTime<endDateTime))
{
...
}
Also greater than or equal to and less than or equal to are supported.
With an overnight window, I don't think there's anything particularly clever to be done except to directly check your DateTime's TimeOfDay against the boundaries:
using System;
namespace Question2355777
{
class Program
{
private static bool IsInOvernightWindow(
DateTime dateTimeUnderTest,
TimeSpan morningEnd,
TimeSpan eveningStart)
{
TimeSpan timeOfDay = dateTimeUnderTest.TimeOfDay;
return timeOfDay <= morningEnd || timeOfDay >= eveningStart;
}
static void Main(string[] args)
{
TimeSpan eveningStart = TimeSpan.FromHours(20);
TimeSpan morningEnd = TimeSpan.FromHours(7);
Console.WriteLine("{0} {1}",
DateTime.Today.AddHours(3),
IsInOvernightWindow(
DateTime.Today.AddHours(3),
morningEnd,
eveningStart));
Console.WriteLine("{0} {1}",
DateTime.Today.AddHours(12),
IsInOvernightWindow(
DateTime.Today.AddHours(12),
morningEnd,
eveningStart));
Console.WriteLine("{0} {1}",
DateTime.Today.AddHours(21),
IsInOvernightWindow(
DateTime.Today.AddHours(21),
morningEnd,
eveningStart));
Console.ReadLine();
}
}
}
produces
01/03/2010 03:00:00 True
01/03/2010 12:00:00 False
01/03/2010 21:00:00 True
// initializing with some sample values
TimeSpan start = TimeSpan.FromHours(20);
TimeSpan end = TimeSpan.FromHours(7);
DateTime now = DateTime.Now.TimeOfDay;
return start<end
? start <= now.TotalHours && now.TotalHours <= end
: start <= now.TotalHours || now.TotalHours <= end;
Use the TimeOfDay method:
DateTime dtStart = new DateTime(0,0,0,20,0,0);
DateTime dtEnd = new DateTime(0,0,0,7,0,0);
if (DateTime.Now.TimeOfDay < dtEnd.TimeOfDay || DateTime.Now.TimeOfDay > dtStart.TimeOfDay)
{
// your code here
}
Count all the times in units of minutes beginning from the start day.
struct DateTime {
uint_8 hour_;
uint_8 minute_;
};
bool
isTimeWithin( DateTime start, DataTime stop, DateTime query ) {
// The following times are counted from the beginning of start day
uint_16 startInMins = (60 * start.hour_ + start.minute_);
// Added 24*60 since "stop" is always "overnight" from "start"
uint_16 stopInMins = 24 * 60 + (60 * stop.hour_ + stop.minute_);
// The ternary operator checks whether "query" is in the same day as
// "start" or the next day
uint_16 queryInMins = (query.hour_ < start.hour_ ? 24 * 60 : 0 ) +
(60 * query.hour_ + query.minute_);
return ((startInMins <= queryInMins) && (queryInMins <= stopInMins));
}
EDIT: Improved Formatting.
if ALL you have are those values for start and stop, don't you have an empty set?
Ignore the obvious assumption - start day X, end day X + Y.
[edit]
since the question has been edited, so shall be the answer....
for start time, end time and 'test' time get the number of milliseconds from the epoch (define that any way you would like)
and then check to see if test is >= start and <= end
[/edit]
I feel like this is math problem more than anything. My company has employees all over the country. Some parts of the company are on an "odd" pay cycle and some are on "even". I call the starting date of a given pay period a "payperiod". I need to do two things:
1) determine the payperiod in which a given date falls
//Something like this:
public static DateTime getPayPeriodStartDate(DateTime givenDate, string EvenOrOdd)
{ .. }
2) get a list of payperiods between two dates:
//Something like this:
public static List<DateTime> getPayPeriodsBetween(DateTime start, DateTime end, string EvenOrOdd)
{ .. }
I'm using a couple dates as fixed standards on which to base any future pay period dates. The fixed standard dates for even and odd are as follows:
Even - 01/04/09
Odd - 01/11/09
Each pay period starts on the sunday of the week and goes for two weeks. For instance, using the standard dates above, the first even pay period starts on 01/04/09 and ends on 01/17/09. The first odd pay period starts on 01/11/09 and ends on 01/24/09. As you can see, there is some overlap. We have thousands of employees so it's necessary to split them up a bit.
I have a solution that is based on week numbers but it's clunky and has to be "fixed" every new year. I'm wondering how you would handle this.
Not fully optimized or tested, but this is what I came up with:
const int DaysInPeriod = 14;
static IEnumerable<DateTime> GetPayPeriodsInRange(DateTime start, DateTime end, bool isOdd)
{
var epoch = isOdd ? new DateTime(2009, 11, 1) : new DateTime(2009, 4, 1);
var periodsTilStart = Math.Floor(((start - epoch).TotalDays) / DaysInPeriod);
var next = epoch.AddDays(periodsTilStart * DaysInPeriod);
if (next < start) next = next.AddDays(DaysInPeriod);
while (next <= end)
{
yield return next;
next = next.AddDays(DaysInPeriod);
}
yield break;
}
static DateTime GetPayPeriodStartDate(DateTime givenDate, bool isOdd)
{
var candidatePeriods = GetPayPeriodsInRange(givenDate.AddDays(-DaysInPeriod), givenDate.AddDays(DaysInPeriod), isOdd);
var period = from p in candidatePeriods where (p <= givenDate) && (givenDate < p.AddDays(DaysInPeriod)) select p;
return period.First();
}
I haven't tested for many test cases, but I think this fits the bill:
public static DateTime getPayPeriodStartDate(DateTime givenDate, string EvenOrOdd)
{
DateTime newYearsDay = new DateTime(DateTime.Today.Year, 1, 1);
DateTime firstEvenMonday = newYearsDay.AddDays((8 - (int)newYearsDay.DayOfWeek) % 7);
DateTime firstOddMonday = firstEvenMonday.AddDays(7);
TimeSpan span = givenDate - (EvenOrOdd.Equals("Even") ? firstEvenMonday : firstOddMonday);
int numberOfPayPeriodsPast = span.Days / 14;
return (EvenOrOdd.Equals("Even") ? firstEvenMonday : firstOddMonday).AddDays(14 * numberOfPayPeriodsPast);
}
public static List<DateTime> getPayPeriodsBetween(DateTime start, DateTime end, string EvenOrOdd)
{
DateTime currentPayPeriod = getPayPeriodStartDate(start, EvenOrOdd);
if (currentPayPeriod < start) currentPayPeriod = currentPayPeriod.AddDays(14);
List<DateTime> dtList = new List<DateTime>();
while (currentPayPeriod <= end)
{
dtList.Add(currentPayPeriod);
currentPayPeriod = currentPayPeriod.AddDays(14);
}
return dtList;
}
I am sure it can be improved.
I had a need to do something similar and was able to do it very easily using LINQ. Simply build up a List for even and odd and then query between dates from the odd/even as necessary. Also, I recommend you move to an emum for parameters like EvenOrOdd where you have fixed values.
I had a similar problem a few months ago, and I ended up writing a quick script to create entries in a database for each pay period so I never had to actually do the math. This way, The system works the same speed, and doesn't have to do any slow iterations every time a period is requested.
That being said, you can always take the starting date, and add two weeks (or however long your periods are) over and over until you reach the dates you specify in the function call. This is a bit ugly, and the longer it sits in production, the slower it gets (since the dates are getting further and further apart).
Both ways are trivial to implement, it's just a matter of what kind of resources you have at hand to tackle the issue.
So, for number 1: Start with either 1/4/2009 or 1/11/2009 (depending on even/odd pay week) and add 2 weeks until the givenDate is less than the date you're testing + 2 weeks. That's the start of the period.
For number 2: Same thing, start at the date and add 2 weeks until you're within the date range. While you're there, add each item to a list. As soon as you're past the last date, break out of your loop and return your shiny new list.
If you used my method and went with a database to house all this info, it turns into 2 simple queries:
1)SELECT * FROM payperiods WHERE startdate<=givenDate ORDER BY startdate LIMIT 1
2) SELECT * FROM payperiods WHERE startdate>=givenDate AND enddate<=givenDate ORDER BY startdate
It works perfectly. I have tested.
public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
{
CultureInfo _culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
CultureInfo _uiculture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();
_culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
_uiculture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
System.Threading.Thread.CurrentThread.CurrentCulture = _culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = _uiculture;
// CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
DayOfWeek firstDay = _culture.DateTimeFormat.FirstDayOfWeek;
DateTime firstDayInWeek = dayInWeek.Date;
// Logic Of getting pay period Monday(Odd monday)
int i = Convert.ToInt32(firstDay);
while (firstDayInWeek.DayOfWeek != firstDay)
if (i % 2 != 0)
{ firstDayInWeek = firstDayInWeek.AddDays(-1); }
else
{
firstDayInWeek = firstDayInWeek.AddDays(-2);
}
return firstDayInWeek;
}