DateTimeExtensions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Abp.Timing;
  5. namespace Abp.Extensions
  6. {
  7. /// <summary>
  8. /// Extension methods for <see cref="DateTime"/>.
  9. /// </summary>
  10. public static class DateTimeExtensions
  11. {
  12. /// <summary>
  13. /// Converts a DateTime to a Unix Timestamp
  14. /// </summary>
  15. /// <param name="target">This DateTime</param>
  16. /// <returns></returns>
  17. public static double ToUnixTimestamp(this DateTime target)
  18. {
  19. var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  20. var diff = target - origin;
  21. return Math.Floor(diff.TotalSeconds);
  22. }
  23. /// <summary>
  24. /// Converts a Unix Timestamp in to a DateTime
  25. /// </summary>
  26. /// <param name="unixTime">This Unix Timestamp</param>
  27. /// <returns></returns>
  28. public static DateTime FromUnixTimestamp(this double unixTime)
  29. {
  30. var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  31. return epoch.AddSeconds(unixTime);
  32. }
  33. /// <summary>
  34. /// Gets the value of the End of the day (23:59)
  35. /// </summary>
  36. /// <param name="target"></param>
  37. /// <returns></returns>
  38. public static DateTime ToDayEnd(this DateTime target)
  39. {
  40. return target.Date.AddDays(1).AddMilliseconds(-1);
  41. }
  42. /// <summary>
  43. /// Gets the First Date of the week for the specified date
  44. /// </summary>
  45. /// <param name="dt">this DateTime</param>
  46. /// <param name="startOfWeek">The Start Day of the Week (ie, Sunday/Monday)</param>
  47. /// <returns>The First Date of the week</returns>
  48. public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
  49. {
  50. var diff = dt.DayOfWeek - startOfWeek;
  51. if (diff < 0)
  52. diff += 7;
  53. return dt.AddDays(-1 * diff).Date;
  54. }
  55. /// <summary>
  56. /// Returns all the days of a month.
  57. /// </summary>
  58. /// <param name="year">The year.</param>
  59. /// <param name="month">The month.</param>
  60. /// <returns></returns>
  61. public static IEnumerable<DateTime> DaysOfMonth(int year, int month)
  62. {
  63. return Enumerable.Range(0, DateTime.DaysInMonth(year, month))
  64. .Select(day => new DateTime(year, month, day + 1));
  65. }
  66. /// <summary>
  67. /// Determines the Nth instance of a Date's DayOfWeek in a month
  68. /// </summary>
  69. /// <returns></returns>
  70. /// <example>11/29/2011 would return 5, because it is the 5th Tuesday of each month</example>
  71. public static int WeekDayInstanceOfMonth(this DateTime dateTime)
  72. {
  73. var y = 0;
  74. return DaysOfMonth(dateTime.Year, dateTime.Month)
  75. .Where(date => dateTime.DayOfWeek.Equals(date.DayOfWeek))
  76. .Select(x => new { n = ++y, date = x })
  77. .Where(x => x.date.Equals(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day)))
  78. .Select(x => x.n).FirstOrDefault();
  79. }
  80. /// <summary>
  81. /// Gets the total days in a month
  82. /// </summary>
  83. /// <param name="dateTime">The date time.</param>
  84. /// <returns></returns>
  85. public static int TotalDaysInMonth(this DateTime dateTime)
  86. {
  87. return DaysOfMonth(dateTime.Year, dateTime.Month).Count();
  88. }
  89. /// <summary>
  90. /// Takes any date and returns it's value as an Unspecified DateTime
  91. /// </summary>
  92. /// <param name="date"></param>
  93. /// <returns></returns>
  94. public static DateTime ToDateTimeUnspecified(this DateTime date)
  95. {
  96. if (date.Kind == DateTimeKind.Unspecified)
  97. {
  98. return date;
  99. }
  100. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, DateTimeKind.Unspecified);
  101. }
  102. /// <summary>
  103. /// Trims the milliseconds off of a datetime
  104. /// </summary>
  105. /// <param name="date"></param>
  106. /// <returns></returns>
  107. public static DateTime TrimMilliseconds(this DateTime date)
  108. {
  109. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
  110. }
  111. }
  112. }