private DispatcherTimer dispatcherTimer; public MainWindow() { InitializeComponent(); dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 当间隔时间过去时发生的事件 dispatcherTimer.Tick += new EventHandler(ShowCurrentTime); dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1); dispatcherTimer.Start(); } public void ShowCurrentTime(object sender, EventArgs e) { //获得星期 //this.tBlockTime.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //this.tBlockTime.Text += " "; //获得年月日 //this.tBlockTime.Text = DateTime.Now.ToString("yyyy:MM:dd"); //yyyy年MM月dd日 //this.tBlockTime.Text += " "; //获得时分秒 this.tBlockTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss dddd"); }
<TextBlock Name="tBlockTime" FontSize="40" Grid.Column="1" HorizontalAlignment="Right" Foreground="Green"/>
运行效果: