public static Class Datetimeextensions
{
public static datetime TOUTC (this datetime time)
{
if (time. Kind = = DATETIMEKIND.UTC)
{
return time;
}
else if (time. Kind = = datetimekind.local)
{
Return time. ToUniversalTime ();
}
Else
{
Return time. ToLocalTime (). ToUniversalTime ();
}
}
private static readonly datetime s_maxdatetime = new DateTime (4000, 1, 1, 1, 1, 1, 1). TOUTC ();
public static DateTime Maxdatetime
{
Get
{
return s_maxdatetime;
}
}
public static bool Ismaxdatetime (this DateTime time)
{
Return time. Year = = Maxdatetime.year;
}
public static DateTime? TOUTC (this DateTime? time)
{
Return time. HasValue? (DateTime?) Time. VALUE.TOUTC (): null;
}
public static string Tochinesedate (this DateTime time)
{
Return time. ToString ("Yyyy-mm-dd");
}
public static string Tochinesedate (this DateTime? time)
{
if (!time. HasValue)
return string. Empty;
Return time. Value.tostring ("Yyyy-mm-dd");
}
public static string Tochinesetime (this DateTime time)
{
Return time. ToString ("Yyyy-mm-dd hh:mm");
}
//<summary>
/ Convert to timestamp
//</summary>
//<param name= "Time" ></PARAM>
//< RETURNS></RETURNS>
public static Int64 Totimestamp (this DateTime time)
{
return (time. ToUniversalTime (). Ticks-621355968000000000)/10000000;
}
public static string toexpirestring (this DateTime time)
{
var now = DateTime.Now;
if (now < time)
return string. Empty;
var offset = (now-time);
if (Offset < new TimeSpan (0, 1, 0))
{
return "just";
}
else if (offset < new TimeSpan (1, 0, 0))
{
return string. Format ("{0} minutes", offset. Minutes);
}
else if (offset < new TimeSpan (1, 0, 0, 0))
{
return string. Format ("{0} hours", offset.) Hours);
}
else if (offset < new TimeSpan (7, 0, 0, 0))
{
return string. Format ("{0} days", offset.) Days);
}
else if (offset < new TimeSpan (30, 0, 0, 0))
{
return string. Format ("{0} Week", offset.) DAYS/7);
}
else if (offset < new TimeSpan (365, 0, 0, 0))
{
return string. Format ("{0} months", offset.) DAYS/30);
}
Else
{
Return time. Tochinesetime ();
}
}
public static datetime Getthismonday (this datetime value)
{
int DayofWeek = value. DayOfWeek = = DayOfWeek.Sunday? 7: (int) value. DayOfWeek;
return value. Date.adddays ((int) dayofweek.monday-dayofweek);
}
public static datetime getthemintime (this datetime time)
{
Return DateTime.Parse (time. ToShortDateString () + "00:00:00");
}
public static datetime getthemaxtime (this datetime time)
{
Return DateTime.Parse (time. ToShortDateString () + "23:59:59");
}
public static string HtmlEncode (this DateTime value, string format)
{
return value. ToString (format). HtmlEncode ();
}
public static string Htmlattrencode (this DateTime value, string format)
{
return value. ToString (format). Htmlattrencode ();
}
public static string UrlEncode (this DateTime value, string format)
{
return value. ToString (format). UrlEncode ();
}
}
Formatting extension functions for Date, datetime values