开发者

Digital clock -

I would like to display a digital clock.Here is开发者_StackOverflow what i have to display the time in a textblock which is in 24 hour format that i dont know how to convert to 12 hours.

DispatcherTimer timerdigital;

    public MainWindow()
    {
        this.InitializeComponent();
        //StartTimer(null, null);

        timerdigital = new DispatcherTimer();
        timerdigital.Interval = TimeSpan.FromSeconds(1.0);
        timerdigital.Start();
        timerdigital.Tick += new EventHandler(delegate(object s, EventArgs a)
        {
            tbDigital.Text = "" + DateTime.Now.Hour + ":"
          + DateTime.Now.Minute + ":"
          + DateTime.Now.Second;


        });

thank you


Don't know about the digital-like display, but for switching to 12-hour clock from 24-hour the easiest approach would be to use DateTime string formatting:

timerdigital.Tick += new EventHandler(delegate(object s, EventArgs a)
        {
            tbDigital.Text = DateTime.Now.ToString("hh:mm:ss tt");
        });

Here hh is the hours in 12-hour format, mm is minutes, ss is seconds, and tt is the AM/PM marker.

See more about Date and Time format strings here.


Here is a nice ready to use source code for wpf:
http://www.codeproject.com/KB/WPF/digitalclock.aspx
About the 12/24 hour format:

 int smallhour = (Hour > 12) ? Hour - 12 : Hour;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜