1

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • 1
    Does this answer your question? [What's the best way to produce a relative date range (This week, This year, Last month, etc) from a DateTime?](https://stackoverflow.com/questions/711121/whats-the-best-way-to-produce-a-relative-date-range-this-week-this-year-last) – gcores Jun 30 '22 at 15:42
  • -gcores That is good stuff and I am going to save it but I am not sure I can build all that onto a Razor page that is looping through displaying. I would have to create a .cs page with the function and it would return the range but I want to know if whatever date is inside the range. I am keeping it but looking for a simpler answer. . . . .if possible thank you very much. – JustJohn Jul 01 '22 at 04:32

1 Answers1

1

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);
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • @PoulBak The week rule doesn't matter as long as we're consistent. We don't actually care about the integer value of the week. – John Wu Jun 30 '22 at 16:17
  • You're right, but first day of the week matters. 'DateTimeFormatInfo.FirstDayOfWeek'. – Poul Bak Jun 30 '22 at 16:20
  • Hmmm. Interesting that that property even exists. In my experience, at least in the United States, the first day of the week depends on a context that is more granular than the culture. Many businesses use Monday as the first day of the week, for example. Some restaurants actually use Tuesday! But I guess on the printed calendar it's usually Sunday. – John Wu Jun 30 '22 at 16:38
  • Well, how can you coexist if you can't agree on the week number :-)? At least in Europe it's consistent. – Poul Bak Jun 30 '22 at 16:41
  • In my many years of experience I can recall only one time someone wanted a week "number." For the most part, weeks are identified by an end date, e.g. at my current job we are currently in "Week ending 7/3/2022". Maybe that's just the U.S. I agree there is a lot of room for inconsistency. Don't even get me started on "fiscal" years vs. "tax" years. – John Wu Jun 30 '22 at 16:43
  • Your second example you called "cheat", is what interests me. . . .. however. I get these errors when pasting it into my Razor code: your line (currentWeekStart <= varNextEvent) && (varNextEvent < nextWeekStart) . . . . creates this error: Operator '<=' cannot be applied to operands of type 'DateTime' and 'string' . . . . . . which was my problem in the first place: how to do do a "=>" and a "<=" in an if statement. – JustJohn Jul 01 '22 at 04:41
  • Oh. What exactly is in `varNextEvent`? Is it not a DateTime? If it's a string, you'll need to [convert it first](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime). – John Wu Jul 01 '22 at 06:21
  • Hi John Wu, thanks for your attention. It was a string date. I converted it to a DateTime date and was able to get the syntax to work. . . . . . . DateTime datNextEvent = Convert.ToDateTime(varNextEvent); . . . . . . .and then after futzing with the correct parenthesis I got it to work. . . . . . @if (datNextEvent >= DateTime.Today && datNextEvent <= DateTime.Today.AddDays(7)) . . . . . .Thank you very much. So the greater than and less than or equal work between date datatypes! I will select your answer whether you edit it slightly to mention this or not. Cheers! You made my day. – JustJohn Jul 01 '22 at 23:37