Print Date - CSharp System

CSharp examples for System:DateTime Calculate

Description

Print Date

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//w w  w.  j  a v a 2s.  c  o m

public class Main{
        //format options are semicoloned.
    public static string PrintDate(DateTime res, string formatoptions)
    {
        string[] t = formatoptions.ToLower().Split(';');

        if (Array.IndexOf(t, "toutc") != -1)
            res = res.ToUniversalTime();
        else if (Array.IndexOf(t, "tolocal") != -1)
            res = res.ToLocalTime();

        if (Array.IndexOf(t, "longtime") != -1)
        {
            return res.ToLongTimeString();
        }
        else if (Array.IndexOf(t, "shorttime") != -1)
        {
            return res.ToShortTimeString();
        }
        else if (Array.IndexOf(t, "longdatetime") != -1)
        {
            return res.ToLongDateString() + " " + res.ToLongTimeString();
        }
        else if (Array.IndexOf(t, "longdate") != -1)
        {
            return res.ToLongDateString();
        }
        else if (Array.IndexOf(t, "datetime") != -1)
        {
            return res.ToShortDateString() + " " + res.ToLongTimeString();
        }
        else if (Array.IndexOf(t, "shortdate") != -1)
        {
            return res.ToShortDateString();
        }
        else
        {
            return res.ToString("yyyy/mm/dd HH:mm:ss");
        }
    }
}

Related Tutorials