C calculates the corresponding Monday and Sunday instance code from the current date

  • 2020-05-17 06:18:32
  • OfStack


/// <summary>
  ///  Calculate the start date of this week (week) 1 The date) 
  /// </summary>
  /// <param name="someDate"> Any time of the week 1 day </param>
  /// <returns> Returns the week 1 The date, followed by specific hours, minutes, and seconds, is equal to the incoming value </returns>
  public static DateTime CalculateFirstDateOfWeek(DateTime someDate)
  {
   int i = someDate.DayOfWeek - DayOfWeek.Monday;
   if (i == -1) i = 6;// i value  > = 0  , for enumeration reasons, Sunday At the top, right now Sunday-Monday=-1 That must be +7=6 . 
   TimeSpan ts = new TimeSpan(i, 0, 0, 0);
   return someDate.Subtract(ts);
  }
  /**//// <summary>
  ///  Calculate the end date of the week (date of Sunday) 
  /// </summary>
  /// <param name="someDate"> Any time of the week 1 day </param>
  /// <returns> Returns the date of the Sunday, followed by the exact hour, minute, and second equal to the incoming value </returns>
  public static DateTime CalculateLastDateOfWeek(DateTime someDate)
  {
   int i = someDate.DayOfWeek - DayOfWeek.Sunday;
   if(i != 0) i = 7 - i;//  For enumeration reasons, Sunday First of all, the subtraction interval has to be 7 Decreases. 
   TimeSpan ts = new TimeSpan(i, 0, 0, 0);
   return someDate.Add(ts);
  }
  /**//// <summary>
  ///  Determine if the selected date is this week (' this week 'as determined by the current time of the system ' By comparison) 
  /// </summary>
  /// <param name="someDate"></param>
  /// <returns></returns>
  public static bool IsThisWeek(DateTime someDate)
  {
   // get someDate The corresponding week 1
   DateTime someMon = CalculateFirstDateOfWeek(someDate);
   // Get this week 1
   DateTime nowMon = CalculateFirstDateOfWeek(DateTime.Now);
   TimeSpan ts = someMon - nowMon;
   if (ts.Days < 0)
    ts = -ts;// Take is 
   if (ts.Days >= 7)
   {
    return false;
   }
   else 
   {
    return true;
   }
  }

Related articles: