Formatting extension functions for Date, datetime values

Source: Internet
Author: User
Tags urlencode

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.