Guest User

Untitled

a guest
Oct 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date
  2.  
  3. ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
  4. If lstOrderedDates.Count > 1 Then
  5. Dim dateToCheck = lstOrderedDates.ElementAt(1)
  6. Dim selectedDate = lstOrderedDates.ElementAt(0)
  7. For i = 1 To lstOrderedDates.Count - 1
  8.  
  9. If dateToCheck < Date.Now.Date Then
  10. Exit For
  11. End If
  12.  
  13. If dateToCheck >= Date.Now.Date Then
  14. selectedDate = dateToCheck
  15. End If
  16.  
  17. If i + 1 < lstOrderedDates.Count Then
  18. dateToCheck = lstOrderedDates.ElementAt(i + 1)
  19. End If
  20. Next i
  21. Return selectedDate
  22. ElseIf lstOrderedDates.Count = 1 Then
  23. Return lstOrderedDates.First()
  24. End If
  25.  
  26. Return Nothing
  27. End Function
  28.  
  29. Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date
  30.  
  31. ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
  32. If lstOrderedDates is Nothing OrElse lstOrderedDates.Count = 0 Then
  33. Return Nothing
  34. ElseIf lstOrderedDates.Count = 1
  35. Return lstOrderedDates.ElementAt(0)
  36. End If
  37. Dim ordered as List(Of DateTime) = lstOrderedDates.OrderByDescending(function(d) d).ToList()
  38. Dim selectedDate = ordered.ElementAt(0)
  39. For i = 1 To ordered.Count - 1
  40.  
  41. Dim dateToCheck = ordered.ElementAt(i)
  42.  
  43. If dateToCheck < Date.Now.Date Then
  44. Exit For
  45. End If
  46.  
  47. selectedDate = dateToCheck
  48.  
  49. Next i
  50. Return selectedDate
  51.  
  52. End Function
  53.  
  54. public static DateTime GetClosetDateToToday(List<DateTime> ordered)
  55. {
  56. if (ordered == null || ordered.Count == 0) { return default(DateTime); }
  57.  
  58. var result = ordered
  59. .OrderByDescending(item => item)
  60. .LastOrDefault(item => item >= DateTime.Now);
  61.  
  62. if (result == default(DateTime))
  63. {
  64. return ordered[0];
  65. }
  66. return result;
  67. }
  68.  
  69. Public Shared Function GetClosetDateToToday(lstOrderedDates As List(Of Date)) As Date
  70.  
  71. ' Select the closest Delivery date to TODAY (inclusive) from the list of Ordered dates
  72. If lstOrderedDates is Nothing OrElse lstOrderedDates.Count = 0 Then
  73. Return Nothing
  74. End If
  75.  
  76. Dim found as DateTime = lstOrderedDates
  77. .OrderByDescending(function(d) d)
  78. .LastOrDefault(function(d) d >= DateTime.Now)
  79. If found is Nothing then
  80. found = lstOrderedDates.ElementAt(0)
  81. End If
  82.  
  83. Return found
  84.  
  85. End Function
Add Comment
Please, Sign In to add comment