daily pastebin goal
67%
SHARE
TWEET

DateTime Functions

jessicakennedy1028 Sep 10th, 2018 (edited) 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public static partial class DateTimeExtensions
  2.     {
  3.         public static DateTime FirstDayOfWeek(this DateTime dt)
  4.         {
  5.             var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
  6.             var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
  7.             if (diff < 0)
  8.                 diff += 7;
  9.             return dt.AddDays(-diff).Date;
  10.         }
  11.  
  12.         public static DateTime LastDayOfWeek(this DateTime dt)
  13.         {
  14.             return dt.FirstDayOfWeek().AddDays(6);
  15.         }
  16.  
  17.         public static DateTime FirstDayOfMonth(this DateTime dt)
  18.         {
  19.             return new DateTime(dt.Year, dt.Month, 1);
  20.         }
  21.  
  22.         public static DateTime LastDayOfMonth(this DateTime dt)
  23.         {
  24.             return dt.FirstDayOfMonth().AddMonths(1).AddDays(-1);
  25.         }
  26.  
  27.         public static DateTime FirstDayOfNextMonth(this DateTime dt)
  28.         {
  29.             return dt.FirstDayOfMonth().AddMonths(1);
  30.         }
  31.  
  32.         public static DateTime FirstDayOfQuarter(this DateTime dt)
  33.         {
  34.             int quarterNumber = (dt.Month - 1) / 3 + 1;
  35.             return new DateTime(dt.Year, (quarterNumber - 1) * 3 + 1, 1);
  36.         }
  37.  
  38.         public static DateTime LastDayOfQuarter(this DateTime dt)
  39.         {
  40.             int quarterNumber = (dt.Month - 1) / 3 + 1;
  41.             return dt.FirstDayOfQuarter().AddMonths(3).AddDays(-1);
  42.         }
  43.     }
  44.  
  45. // How to use it, you can now place this in your namespace:
  46. string MessageString = "";
  47. var today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
  48. var yesterday = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(-1);
  49. var firstdayOfThisWeek = today.FirstDayOfWeek();                                // 9/9/2018 12:00:00 AM
  50. var firstdayOfLastWeek = today.AddDays(-7).FirstDayOfWeek();                    // 9/2/2018 12:00:00 AM
  51. var lastDayOfThisWeek = today.LastDayOfWeek();                                  // 9/15/2018 12:00:00 AM
  52. var lastDayOfLastWeek = today.AddDays(-7).LastDayOfWeek();                      // 9/8/2018 12:00:00 AM
  53. var firstdayOfThisMonth = today.FirstDayOfMonth();                              // 9/1/2018 12:00:00 AM
  54. var firstdayOfLastMonth = today.AddMonths(-1).FirstDayOfMonth();                // 8/1/2018 12:00:00 AM
  55. var lastdayOfThisMonth = today.LastDayOfMonth();                                // 9/30/2018 12:00:00 AM
  56. var lastdayOfLastMonth = today.AddMonths(-1).LastDayOfMonth();                  // 8/31/2018 12:00:00 AM
  57. var firstdayOfThisQuarter = today.FirstDayOfQuarter();                          // 7/1/2018 12:00:00 AM
  58. var firstdayOfLastQuarter = today.AddMonths(-3).FirstDayOfQuarter();            // 4/1/2018 12:00:00 AM
  59. var lastdayOfThisQuarter = today.LastDayOfQuarter();                            // 9/30/2018 12:00:00 AM
  60. var lastdayOfLastQuarter = today.AddMonths(-3).LastDayOfQuarter();              // 6/30/2018 12:00:00 AM
  61. var beginingOfThisYear = new DateTime(DateTime.Now.Year, 1, 1);                 // 1/1/2018 12:00:00 AM
  62. var beginingOfLastYear = new DateTime(DateTime.Now.Year, 1, 1).AddYears(-1);    // 1/1/2017 12:00:00 AM
  63. var endOfThisYear = new DateTime(DateTime.Now.Year, 12, 31);                    // 12/31/2018 12:00:00 AM
  64. var endOfLastYear = new DateTime(DateTime.Now.Year, 12, 31).AddYears(-1);       // 12/31/2017 12:00:00 AM
  65.  
  66. MessageString = "Today is :" + today + "\r\n";
  67. MessageString = MessageString + "Yesterday :" + yesterday + "\r\n";
  68. MessageString = MessageString + "First Day of this Week is :" + firstdayOfThisWeek + "\r\n";
  69. MessageString = MessageString + "First Day of last Week is :" + firstdayOfLastWeek + "\r\n";
  70. MessageString = MessageString + "Last Day of this Week is : " + lastDayOfThisWeek + "\r\n";
  71. MessageString = MessageString + "Last Day of last Week is : " + lastDayOfLastWeek + "\r\n";
  72. MessageString = MessageString + "First Day of this Month is : " + firstdayOfThisMonth + "\r\n";
  73. MessageString = MessageString + "First Day of last Month is : " + firstdayOfLastMonth + "\r\n";
  74. MessageString = MessageString + "Last Day of this Month : " + lastdayOfThisMonth + "\r\n";
  75. MessageString = MessageString + "Last Day of last Month : " + lastdayOfLastMonth + "\r\n";
  76. MessageString = MessageString + "First Day of this Quarter : " + firstdayOfThisQuarter + "\r\n";
  77. MessageString = MessageString + "First Day of last Quarter : " + firstdayOfLastQuarter + "\r\n";
  78. MessageString = MessageString + "Last Day of last Quarter : " + lastdayOfThisQuarter + "\r\n";
  79. MessageString = MessageString + "Last Day of last Quarter : " + lastdayOfLastQuarter + "\r\n";
  80. MessageString = MessageString + "First Day of last Year : " + beginingOfThisYear + "\r\n";
  81. MessageString = MessageString + "First Day of last Year : " + beginingOfLastYear + "\r\n";
  82. MessageString = MessageString + "Last Day of last Year : " + endOfThisYear + "\r\n";
  83. MessageString = MessageString + "Last Day of last Year : " + endOfLastYear + "\r\n";
  84. MessageBox.Show(MessageString);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top