加班计时App

时间:2024-01-21 18:22:21
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. using System.IO;
  17. namespace TimeUpGoHome
  18. {
  19. /// <summary>
  20. /// MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. private string shouldGoHomeTime = "18:15";
  25. private string trueGoHomeTime = "";
  26. private DispatcherTimer ShowTimer;
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. ShowTimer = new System.Windows.Threading.DispatcherTimer();
  31. ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
  32. ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
  33. ShowTimer.Start();
  34. this.MyText.Text = "";
  35. }
  36. public void ShowCurTimer(object sender, EventArgs e)
  37. {
  38. //"星期"+DateTime.Now.DayOfWeek.ToString(("d"))
  39. //获得星期几
  40. // this.Tt2.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
  41. //his.Tt.Text += " ";
  42. //获得年月日
  43. this.Tt2.Text = DateTime.Now.ToString("yyyy年MM月dd日"); //yyyy年MM月dd日
  44. // this.Tt.Text += " ";
  45. //获得时分
  46. this.Tt.Text = DateTime.Now.ToString("HH:mm");
  47. //System.Diagnostics.Debug.Print("this.ShowCurrentTime {0}", this.ShowCurrentTime);
  48. }
  49. private int[] GetHourAndMin(string _timer){
  50. int n = int.Parse(_timer.Substring(0, 2));
  51. int m = int.Parse(_timer.Substring(3,2));
  52. int[] to_back = new int[] { n,m};
  53. return to_back;
  54. }
  55. private int[] GetRemainHourAndMin(int smallMins1,int bigMins2){
  56. int remainMins= bigMins2-smallMins1;
  57. int remainHours=remainMins/60;
  58. int remainRemainMins=remainMins%60;
  59. int[] to_back=new int[]{remainHours,remainRemainMins};
  60. return to_back;
  61. }
  62. private int GetMins(int[] timeTable)
  63. {
  64. return timeTable[0] * 60 + timeTable[1];
  65. }
  66. private void Button_Click(object sender, RoutedEventArgs e)
  67. {
  68. string today_date = this.Tt2.Text;
  69. string trueBackHomeTime = this.Tt.Text;
  70. int[] shouldBackTimeTable = GetHourAndMin(shouldGoHomeTime);
  71. int[] trueBackTimeTable = GetHourAndMin(trueBackHomeTime);
  72. int shouldMins=GetMins(shouldBackTimeTable);
  73. int trueMins=GetMins(trueBackTimeTable);
  74. if (trueMins < shouldMins)
  75. {
  76. this.MyText.Text="时间还没到!!";
  77. }
  78. else
  79. {
  80. this.MyText.Text="";
  81. int[] remainTimes = GetRemainHourAndMin(shouldMins, trueMins);
  82. using
  83. (StreamWriter iFile = new StreamWriter("D:/AfterWorkLog/workLog.txt", true))
  84. iFile.WriteLine("加班时间:" + today_date+" "+ shouldGoHomeTime + " 到 " + trueBackHomeTime + "; 累计:" +remainTimes[0]+"小时 "+remainTimes[1]+ " 分钟");
  85. App.Current.Shutdown();
  86. }
  87. }
  88. }
  89. }
加班计时App