Common codes for ASP. NET date operations

Source: Internet
Author: User

Using system;

Namespace my. utils {
/// <Summary>
/// Common tools for date operations
/// </Summary>
Public class dateutils {
Public dateutils (){
}

/// <Summary>
/// Compare the year difference of the date
/// </Summary>
/// <Param name = "start"> Start date </param>
/// <Returns> year difference from the current date </returns>
Public static int diffyear (string start ){
Return diffyear (convert. todatetime (start ));
}

/// <Summary>
/// Compare the year difference of the date
/// </Summary>
/// <Param name = "start"> Start date </param>
/// <Param name = "end"> end date </param>
/// <Returns> year difference </returns>
Public static int diffyear (string start, string end ){
Return diffyear (convert. todatetime (start), convert. todatetime (end ));
}

/// <Summary>
/// Compare the year difference of the date
/// </Summary>
/// <Param name = "start"> Start date </param>
/// <Returns> year difference from the current date </returns>
Public static int diffyear (datetime start ){
Return (diffyear (START, datetime. Now ));
}

/// <Summary>
/// Compare the year difference between two dates
/// </Summary>
/// <Param name = "start"> Start date </param>
/// <Param name = "end"> end date </param>
/// <Returns> year difference </returns>
Public static int diffyear (datetime start, datetime end ){
Return (end. Year-start. year );
}

/// <Summary>
/// Format the date of the current day (yyyy-mm-dd)
/// </Summary>
/// <Returns> formatted date string </returns>
Public static string dateformat (){
Return dateformat (datetime. Now );
}

/// <Summary>
/// Format the date (yyyy-mm-dd)
/// </Summary>
/// <Param name = "date"> date to be formatted </param>
/// <Returns> formatted date string </returns>
Public static string dateformat (string date ){
Return dateformat (convert. todatetime (date ));
}

/// <Summary>
/// Format the date
/// </Summary>
/// <Param name = "date"> date to be formatted </param>
/// <Param name = "format"> Format String </param>
/// <Returns> formatted date string </returns>
Public static string dateformat (string date, string format ){
Return dateformat (convert. todatetime (date), format );
}

/// <Summary>
/// Format the date (yyyy-mm-dd)
/// </Summary>
/// <Param name = "date"> date to be formatted </param>
/// <Returns> formatted date string </returns>
Public static string dateformat (datetime date ){
Return dateformat (date, "yyyy-mm-dd ");
}

/// <Summary>
/// Format the date
/// </Summary>
/// <Param name = "date"> date to be formatted </param>
/// <Param name = "format"> Format String </param>
/// <Returns> formatted date string </returns>
Public static string dateformat (datetime date, string format ){
Return date. tostring (format );
}

/// <Summary>
/// Format the date and time
/// </Summary>
/// <Param name = "datetime"> date and time to be formatted </param>
/// <Returns> formatted Date and Time string </returns>
Public static string datetimeformat (datetime ){
Return datetimeformat (datetime, "yyyy-mm-dd hh: mm ");
}

/// <Summary>
/// Format the date and time
/// </Summary>
/// <Param name = "datetime"> date and time to be formatted </param>
/// <Param name = "format"> Format String </param>
/// <Returns> formatted Date and Time string </returns>
Public static string datetimeformat (datetime, string format ){
Return datetime. tostring (format );
}
}
}

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.