DateTime to short and long String
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
namespace ConsultantTime.Web.Utilities.Time
{
public static class DateTimeExtensions
{
public static int WeekNo(this DateTime inDate)
{
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(inDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
}
public static string ToReportString(this DateTime inDate)
{
return String.Format("{0} {1} {2} {3}", inDate.GetDayNameShort(), inDate.Day, inDate.GetMonthNameShort(), inDate.Year);
}
public static string GetDayNameShort(this DateTime inDate)
{
switch (inDate.DayOfWeek)
{
case DayOfWeek.Monday: return "mon"; break;
case DayOfWeek.Tuesday: return "tue"; break;
case DayOfWeek.Wednesday: return "wed"; break;
case DayOfWeek.Thursday: return "thu"; break;
case DayOfWeek.Friday: return "fri"; break;
case DayOfWeek.Saturday: return "sat"; break;
case DayOfWeek.Sunday: return "sun"; break;
default: return ""; break;
}
}
public static string GetDayNameLong(this DateTime inDate)
{
switch (inDate.DayOfWeek)
{
case DayOfWeek.Monday: return "monday"; break;
case DayOfWeek.Tuesday: return "tuesday"; break;
case DayOfWeek.Wednesday: return "wednesday"; break;
case DayOfWeek.Thursday: return "thursday"; break;
case DayOfWeek.Friday: return "friday"; break;
case DayOfWeek.Saturday: return "saturday"; break;
case DayOfWeek.Sunday: return "sunday"; break;
default: return ""; break;
}
}
public static string GetMonthNameShort(this DateTime inDate)
{
switch (inDate.Month)
{
case 1: return "jan"; break;
case 2: return "feb"; break;
case 3: return "mar"; break;
case 4: return "apr"; break;
case 5: return "may"; break;
case 6: return "jun"; break;
case 7: return "jul"; break;
case 8: return "aug"; break;
case 9: return "sep"; break;
case 10: return "oct"; break;
case 11: return "nov"; break;
case 12: return "dec"; break;
default: return ""; break;
}
}
public static string GetMonthNameLong(this DateTime inDate)
{
switch (inDate.Month)
{
case 1: return "january"; break;
case 2: return "february"; break;
case 3: return "march"; break;
case 4: return "april"; break;
case 5: return "may"; break;
case 6: return "june"; break;
case 7: return "july"; break;
case 8: return "august"; break;
case 9: return "september"; break;
case 10: return "october"; break;
case 11: return "november"; break;
case 12: return "december"; break;
default: return ""; break;
}
}
}
}
Related examples in the same category