Convert string to Time by format - CSharp System

CSharp examples for System:DateTime

Description

Convert string to Time by format

Demo Code


using System.Text;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System;//from w ww . ja v  a2  s  . c  o m

public class Main{
        /// <summary>
        ///  Convert Time
        /// </summary>
        /// <param name="Time">Time to convert</param>
        /// <param name="TimeFormat">string format time</param>
        /// <returns>string Time format as TimeFormat</returns>
        public static string ConvertTime(DateTime Time, string TimeFormat)
        {
            return Time.ToString(TimeFormat);
        }
        /// <summary>
        ///  Convert Time
        /// </summary>
        /// <param name="Time">Time to convert</param>
        /// <returns>Default HH:mm:ss</returns>
        public static string ConvertTime(DateTime Time)
        {
            return ConvertTime(Time, "HH:mm:ss");
        }
        /// <summary>
        ///  Convert string Time
        /// </summary>
        /// <param name="Time">string Time to convert</param>
        /// <param name="TimeFormat">string format time</param>
        /// <returns>string Time format as TimeFormat</returns>
        public static string ConvertTime(string Time, string TimeFormat)
        {
            DateTime dt = Convert.ToDateTime(Time);
            return dt.ToString(TimeFormat);
        }
        /// <summary>
        ///  Convert string Time
        /// </summary>
        /// <param name="Time">string Time to convert</param>
        /// <returns>Default HH:mm:ss</returns>
        public static string ConvertTime(string Time) 
        {
            return ConvertTime(Time, "HH:mm:ss");
        }
}

Related Tutorials