DateTimeExtension.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Globalization;
  7. namespace EasyDevCore.Common
  8. {
  9. /// <summary>
  10. /// DateTime extension helper
  11. /// </summary>
  12. public static class DateTimeExtensions
  13. {
  14. /// <summary>
  15. /// Parses by format.
  16. /// </summary>
  17. /// <param name="input">The input.</param>
  18. /// <param name="format">The format.</param>
  19. /// <returns></returns>
  20. public static DateTime ParseByFormat(this DateTime input, string format)
  21. {
  22. return DateTime.ParseExact(input.ToString(format), format, System.Globalization.CultureInfo.InvariantCulture);
  23. }
  24. /// <summary>
  25. /// Gets the week of month.
  26. /// </summary>
  27. /// <param name="time">The time.</param>
  28. /// <returns></returns>
  29. public static int GetWeekOfMonth(this DateTime time)
  30. {
  31. DateTime first = new DateTime(time.Year, time.Month, 1);
  32. return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
  33. }
  34. /// <summary>
  35. /// Gets the week of year.
  36. /// </summary>
  37. /// <param name="time">The time.</param>
  38. /// <returns></returns>
  39. public static int GetWeekOfYear(this DateTime time)
  40. {
  41. return new GregorianCalendar().GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
  42. }
  43. }
  44. }