Get the first day of the current week

Here is a quick way to get the first day of the current week.

If you are happy with sunday as the first day of the week, then you can simply do this :


 public static DateTime FirstDayOfWeek(this DateTime date)
 {
            return DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
 }

But if you want that the first day of the week matches your culture (eg: the belgian week starts on monday), you have to take care of the culture’s DateTime format.

So the method will change a bit :


public static DateTime FirstDayOfWeek(this DateTime date)
{
     return DateTime.Today.AddDays(-((int)DateTime.Today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek));
}

Usage :

    DateTime FirstDayOfWeek = DateTime.Today.FirstDayOfWeek();

or

 DateTime FirstDayOfWeek = DateTime.Now.FirstDayOfWeek();

Incoming search terms:

  • asp net get first day of week
  • first day of week in asp net
  • get first day of the week in linq
  • jquery get first day of current week
  • linq how to get the first day of a week
  • net first day of week

Get the parent Folder for a SPListItem

To retrieve the parent folder

my extension method


public static SPFolder GetParentFolder(this SPListItem spListItem)

{

if (spListItem == null)

return null;

SPFile spFile = spListItem.Web.GetFile(spListItem.Url);

if (spFile == null)

return null;

return spFile.ParentFolder;

}

Use


SPFolder parentFolder = sPListItem.GetParentFolder();

if (parentFolder != null)

{

// do some stuffs..

}

public static SPFolder GetParentFolder(this SPListItem spListItem)
{
if (spListItem == null)
return null;
SPFile spFile = spListItem.Web.GetFile(spListItem.Url);
if (spFile == null)
return null;
return spFile.ParentFolder;
}[/csharp]

Incoming search terms:

  • splistitem parent folder
  • splistitem get parent folder
  • splistitem get containg spfolder
  • splistitem change folder
  • get parent folder of splistitem
  • SPListItem parentfolder
  • how to get splistitem parent folder
  • get SPListItem parent folder
  • get splistitem folder
  • get parent folder for spitem