CSharp examples for System:DateTime Second
Return a string containing a the timestamp formatted to include the exact time of day (to the millisecond).
using System.Collections; using System;//w ww. j a va2 s. c om public class Main{ /// <summary> /// Return a string containing a the timestamp formatted to /// include the exact time of day (to the millisecond). /// </summary> /// <param name="date">The DateTime object to generate the precise time for</param> /// <returns>The formatted timestamp string</returns> public static string PreciseTime(DateTime date) { int hh = date.Hour; int mm = date.Minute; int ss = date.Second; string tod = "AM"; if (hh > 12) { tod = "PM"; hh -= 12; } string hour = Convert.ToString(hh); if (hh < 10) hour = "0" + hour; string min = Convert.ToString(mm); string sec = Convert.ToString(ss); string msec; if (mm < 10) msec = "000" + Convert.ToString(mm); else if (mm < 100) msec = "00" + Convert.ToString(mm); else if (mm < 1000) msec = "0" + Convert.ToString(mm); else msec = Convert.ToString(mm); System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(date.ToShortDateString()) .Append(" ") .Append(hour) .Append(":") .Append(min) .Append(":") .Append(sec) .Append(msec) .Append(" ") .Append(tod); return sb.ToString(); } /// <summary> /// Return a string containing a the timestame formatted to /// include the exact time of day (to the millisecond). /// </summary> /// <returns>The formatted timestamp string</returns> public static string PreciseTime() { return PreciseTime(DateTime.Now); } }