using System; public class ToString { public static void Main() { TimeSpan span; // Initialize a time span to zero. span = TimeSpan.Zero; Console.WriteLine(FormatTimeSpan(span, true)); } private static string FormatTimeSpan(TimeSpan span, bool showSign) { string sign = String.Empty; if (showSign && (span > TimeSpan.Zero)) sign = "+"; return sign + span.Days.ToString("00") + "." + span.Hours.ToString("00") + ":" + span.Minutes.ToString("00") + ":" + span.Seconds.ToString("00") + "." + span.Milliseconds.ToString("000"); } }
Author: coder
TimeSpan.TicksPerDay
using System; using System.Globalization; class MainClass { public static void Main() { // Create some date/time objects DateTime dt = new DateTime(); DateTime dt1 = new DateTime(2001, 12, 31); DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59); // Do some date time math DateTime today = DateTime.Today; today = today + new TimeSpan(TimeSpan.TicksPerDay); Console.WriteLine("Tomorrow is: {0}", today.ToString()); today = DateTime.Today - new TimeSpan(7 * TimeSpan.TicksPerDay); Console.WriteLine("Last Week on this day it was: {0}",today.ToString()); } }
new TimeSpan(2, 12, 0, 0)
using System; class MainClass { public static void Main() { TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0); TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0); TimeSpan oneWeek = timespan1 + timespan2; DateTime now = DateTime.Now; DateTime past = now - oneWeek; DateTime future = now + oneWeek; Console.WriteLine("Now : {0}", now); Console.WriteLine("Past : {0}", past); Console.WriteLine("Future: {0}", future); } }
Timer.Elapsed
using System; using System.Collections.Generic; using System.Text; using System.Timers; class Program { static int counter = 0; static string displayString = "This string will appear one letter at a time. "; static void Main(string[] args) { Timer myTimer = new Timer(100); myTimer.Elapsed += new ElapsedEventHandler(WriteChar); myTimer.Start(); Console.ReadKey(); } static void WriteChar(object source, ElapsedEventArgs e) { Console.Write(displayString[counter++ % displayString.Length]); } }
Interval, Tick, Stop
using System; using System.Drawing; using System.Windows.Forms; class CloseInFive: Form { public static void Main() { Application.Run(new CloseInFive()); } public CloseInFive() { Text = "Closing in Five Minutes"; Timer timer = new Timer(); timer.Interval = 5 * 60 * 1000; timer.Tick += new EventHandler(TimerOnTick); timer.Enabled = true; } void TimerOnTick(object obj, EventArgs ea) { Timer timer = (Timer) obj; timer.Stop(); timer.Tick -= new EventHandler(TimerOnTick); Close(); } }
Simple Clock
using System; using System.Drawing; using System.Windows.Forms; class SimpleClock: Form { public static void Main() { Application.Run(new SimpleClock()); } public SimpleClock() { Timer timer = new Timer(); timer.Tick += new EventHandler(TimerOnTick); timer.Interval = 1000; timer.Start(); } private void TimerOnTick(object sender, EventArgs ea) { Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Center; strfmt.LineAlignment = StringAlignment.Center; pea.Graphics.DrawString(DateTime.Now.ToString("F"), Font, new SolidBrush(ForeColor), ClientRectangle, strfmt); } }
Digital Clock with Date
using System; using System.Drawing; using System.Windows.Forms; class DigitalClock: Form { public static void Main() { Application.Run(new DigitalClock()); } public DigitalClock() { ResizeRedraw = true; Timer timer = new Timer(); timer.Tick += new EventHandler(TimerOnTick); timer.Interval = 1000; timer.Start(); } private void TimerOnTick(object obj, EventArgs ea) { Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; DateTime dt = DateTime.Now; string strTime = dt.ToString("d") + " " + dt.ToString("T"); SizeF sizef = grfx.MeasureString(strTime, Font); float fScale = Math.Min(ClientSize.Width / sizef.Width, ClientSize.Height / sizef.Height); Font font = new Font(Font.FontFamily, fScale * Font.SizeInPoints); StringFormat strfmt = new StringFormat(); strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center; grfx.DrawString(strTime, font, new SolidBrush(ForeColor), ClientRectangle, strfmt); } }