C# class DateTimeExtensions

using System;
using System.Globalization;

public static partial class DateTimeExtensions
{
public static DateTime FirstDayOfWeek(this DateTime dt)
{
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
if (diff < 0)
diff += 7;
return dt.AddDays(-diff).Date;
}

public static DateTime LastDayOfWeek(this DateTime dt)
{
return dt.FirstDayOfWeek().AddDays(6);
}

public static DateTime FirstDayOfMonth(this DateTime dt)
{
return new DateTime(dt.Year, dt.Month, 1);
}

public static DateTime LastDayOfMonth(this DateTime dt)
{
return dt.FirstDayOfMonth().AddMonths(1).AddDays(-1);
}

public static DateTime FirstDayOfNextMonth(this DateTime dt)
{
return dt.FirstDayOfMonth().AddMonths(1);
}


public static int WeekofSelectedDatetime(this DateTime dt)
{
int x= 1;
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;

x = culture.Calendar.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

return x;
}

public static int SelectedMonthNumber(this DateTime dt)
{
int x = 0;
x = dt.Month;
return x;
}

public static string SelectedMonthNameTurkishForLogTables(this DateTime dt)
{
string x = "";
switch (dt.Month)
{
case 1:
x = "Ocak";
break;
case 2:
x = "Subat";
break;
case 3:
x = "Mart";
break;
case 4:
x = "Nisan";
break;
case 5:
x = "Mayis";
break;
case 6:
x = "Haziran";
break;
case 7:
x = "Temmuz";
break;
case 8:
x = "Agustos";
break;
case 9:
x = "Eylul";
break;
case 10:
x = "Ekim";
break;
case 11:
x = "Kasim";
break;
case 12:
x = "Aralik";
break;
default:
break;
}


return x;
}







}

Yorumlar