Guest User

Untitled

a guest
Jul 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Wcm.PublicWeb.Skeleton.Wip.Campaign.Templates
  4. {
  5. public class PrettyDate
  6. {
  7. public string TextJustNow { get; set; }
  8. public string TextOneMinuteAgo { get; set; }
  9. public string TextMinutesAgo { get; set; }
  10. public string TextOneHourAgo { get; set; }
  11. public string TextHoursAgo { get; set; }
  12. public string TextYesterDay { get; set; }
  13. public string TextDaysAgo { get; set; }
  14. public string TextWeeksAgo { get; set; }
  15.  
  16. public PrettyDate()
  17. {
  18. this.TextJustNow = "just now";
  19. this.TextOneMinuteAgo = "1 minute ago";
  20. this.TextMinutesAgo = "{0} minutes ago";
  21. this.TextHoursAgo = "{0} hours ago";
  22. this.TextOneHourAgo = "1 hour ago";
  23. this.TextYesterDay = "yesterday";
  24. this.TextDaysAgo = "{0} days ago";
  25. this.TextWeeksAgo = "{0} weeks ago";
  26. }
  27.  
  28. public string GetPrettyDate(DateTime d)
  29. {
  30.  
  31. var s = DateTime.Now.Subtract(d);
  32. var dayDiff = (int)s.TotalDays;
  33. var secDiff = (int)s.TotalSeconds;
  34.  
  35. if (dayDiff == 0)
  36. {
  37. // Less than one minute ago.
  38. if (secDiff < 60)
  39. {
  40. return this.TextJustNow;
  41. }
  42.  
  43. // Less than 2 minutes ago.
  44. if (secDiff < 120)
  45. {
  46. return this.TextOneMinuteAgo;
  47. }
  48.  
  49. // Less than one hour ago.
  50. if (secDiff < 3600)
  51. {
  52. return string.Format(this.TextMinutesAgo, Math.Floor((double)secDiff / 60));
  53. }
  54.  
  55. // Less than 2 hours ago.
  56. if (secDiff < 7200)
  57. {
  58. return this.TextOneHourAgo;
  59. }
  60.  
  61. // Less than one day ago.
  62. if (secDiff < 86400)
  63. {
  64. return string.Format(this.TextHoursAgo, Math.Floor((double)secDiff / 3600));
  65. }
  66. }
  67.  
  68. // Handle previous days.
  69. if (dayDiff == 1)
  70. {
  71. return this.TextYesterDay;
  72. }
  73.  
  74. if (dayDiff < 7)
  75. {
  76. return string.Format(this.TextDaysAgo, dayDiff);
  77. }
  78.  
  79. return string.Format(this.TextWeeksAgo, Math.Ceiling((double)dayDiff / 7));
  80. }
  81. }
  82. }
Add Comment
Please, Sign In to add comment