To the friendly date string. - CSharp System

CSharp examples for System:DateTime Format

Description

To the friendly date string.

Demo Code


using System.Globalization;
using System;/*from   w ww.j a  v  a2s. c o  m*/

public class Main{
        /// <summary>
        ///     To the friendly date string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public static string ToFriendlyDateString(this DateTime input)
        {
            string formattedDate;

            if (input.Date == DateTime.Today)
            {
                formattedDate = "Today";
            }
            else if (input.Date == DateTime.Today.AddDays(-1))
            {
                formattedDate = "Yesterday";
            }
            else if (input.Date > DateTime.Today.AddDays(-6))
            {
                // *** Show the Day of the week
                formattedDate = input.ToString("dddd");
            }
            else
            {
                formattedDate = input.ToString("MMMM dd, yyyy");
            }

            //append the time portion to the output
            formattedDate += " @ " + input.ToString("t").ToLower();

            return formattedDate;
        }
}

Related Tutorials