fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. // your code goes here
  8. bool test1 = NowWithinShiftTime("08:00 AM", "05:00 PM");
  9. bool test2 = NowWithinShiftTime("05:00 PM", "10:00 PM");
  10. bool test3 = NowWithinShiftTime("09:00 PM", "05:00 AM");
  11. bool test4 = NowWithinShiftTime("02:00 PM", "03:00 AM");
  12. Console.WriteLine("test1: " + test1 + " test2: " + test2 + " test3: " + test3 + " test4: " + test4);
  13. }
  14.  
  15. static bool NowWithinShiftTime(string shiftStart, string shiftEnd)
  16. {
  17. DateTime startDate;
  18. DateTime endDate;
  19. DateTime now = DateTime.Now;
  20.  
  21. TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay;
  22. TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay;
  23.  
  24. if (startTime < endTime) // same day
  25. {
  26. startDate = DateTime.Today + startTime;
  27. endDate = DateTime.Today + endTime;
  28. }
  29. else // next day
  30. {
  31. startDate = DateTime.Today + startTime;
  32. endDate = DateTime.Today.AddDays(1) + endTime;
  33. }
  34. if (now >= startDate && now <= endDate)
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. }
Success #stdin #stdout 0.07s 34760KB
stdin
Standard input is empty
stdout
test1: False test2: False test3: True test4: True