Home > front end >  Code works for "Today's Date" but I need to figure out for "This Week"
Code works for "Today's Date" but I need to figure out for "This Week"

Time:07-02

I loop through on my Razor page and turn the displayed row Tan if it meets the criteria of "Today". How do I test to see if the date falls inside of "This Week"? I know it has something to do with the "Between" syntax of "AddDays" but I can't seem to get it.

@foreach (var item in Model.Dances_Moderated)
{

    DateStuff obj = new DateStuff();

    var varNextEvent = obj.fNextEvent(item.RecurYesNo, item.SingleDate, item.RecurWeekOfMonth, item.RecurDayOfWeek);
    string bgcolor = "Transparent";

    @if (varNextEvent.Equals(DateTime.Today.ToShortDateString()))
    {
        bgcolor = "tan";
    }

EDIT

I guess I was looking for a simple syntax built on the

@if (varNextEvent.Equals(DateTime.Today.ToShortDateString()))

But it won't take this:

 if(varNextEvent => DateTime.Today.ToShortDateString()) && (varNextEvent <= DateTime.Today.AddDays(6).ToShortDateString())

Just a simple "Between" used in many other languages. "More than today and Less than 7 days from now". But I keep getting this error:

Operator '>='(and '<=') cannot be applied to operands of type DateTime and string

Only operator I am given is .Equals.

If worse comes to worse I can just put 7 if statements and using .AddDays(n) I will just add one for each one:

@if (varNextEvent.Equals(DateTime.Today.AddDays(0).ToShortDateString()))
{
    bgcolor = "tan";
}
@else if (varNextEvent.Equals(DateTime.Today.AddDays(1).ToShortDateString()))
{
    bgcolor = "tan";
}
    @else if (varNextEvent.Equals(DateTime.Today.AddDays(2).ToShortDateString()))
{
    bgcolor = "tan";
}

Man, whoever invented this stuff never used it.

CodePudding user response:

The notion of "same week" is particular to the type of calendar. So first you have to get a calendar that represents the culture you're interested in.

From there, you can use GetWeekOfYear to determine what week a date falls on. You will need to decide if the week starts on Sunday or some other day.

//Get calendar for the right culture
var culture = new CultureInfo("en-US");
var calendar = culture.Calendar;

//Get the week # of the current day per the system clock
int currentWeek = calendar.GetWeekOfYear(DateTime.Today, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);

//Get the week # of the event to compare it to
int weekOfEvent = calendar.GetWeekOfYear(varNextEvent , CalendarWeekRule.FirstDay, DateOfWeek.Sunday);

//Check if they're the same
bool isSameWeek = (currentWeek == weekOfEvent);

That all being said, if you're sure ahead of time if you're using the standard western Gregorian calendar, and you are okay with weeks starting on a Sunday, you can cheat by using a little math.

var currentWeekStart = Datetime.Today.AddDays(0 - DateTime.Today.DayofWeek);
var nextWeekStart = currentWeekStart.AddDays(7);
bool isSameWeek = (currentWeekStart <= varNextEvent) && (varNextEvent  < nextWeekStart);
  • Related