Convert a PHP date function into C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Coats

    #1

    Convert a PHP date function into C#

    I have the following function in php which works out a cut of date which is the Tuesday the week prior to the actual date. As I am new to C#, any suggestions on how to do similar in C#?

    Code:
    function getCutOffDate ($tDate) {
    	$cDays = (int) date("w",strtotime($tDate));
    	return strtotime($tDate." -".($cDays + 5)." days");
    }
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    What you are going to want to do is look at the DateTime structure.

    It has two things that are going to be helpful DayOfWeek property and the DaysAdd method. So basic psudocode...

    Code:
    public static DateTime getCutOffDate(DateTime date)
    {
    //figure out which day of the week we are on
    
    //figure out difference between that day and last Tuesday
    
    //use the DateAdd function to compute our new date
    }
    Here is the link to MSDN DateTime Structure

    http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

    Also DayOfWeek returns an enumeration so if you cast it to an int you can use add a value to it.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Simply put, first you get the number of days to subtract, then you subtract them.

      You've already discovered that you have to subtract (day of week + 5) days to get to last Tuesday.

      Now all you need to know is how to subtract:

      Code:
      private DateTime GetCutOffDate(DateTime date)
      {
          int days = (int)date.DayOfWeek + 5;
          return date.Subtract(TimeSpan.FromDays(days));
      }
      You can add and subtract TimeSpans from DateTimes. TimeSpan has several factory methods to create one from a given interval. In the example, we subtracted a TimeSpan created from Days. So, since today is THursday, for instance, that TimeSpan would be 9 days (Thursday's DayOfWeek is 4, + 5 to go back to Tuesday).

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        I was thinking something more like this myself..

        Code:
        public static DateTime GetCutOffDate(DateTime date)
        {
           return date.AddDay(-1 * ((int(date.DayOfWeek)+5)));
        }

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          I prefer the Add and Subtract methods myself, instead of the Add[Days/Hours/Minutes/Whatever], because you can create a timespan of any size, and it will work in Add or Subtract.

          Comment

          • Alan Coats

            #6
            Thanks for the feedback. I will look into it.

            Comment

            • Alan Coats

              #7
              I found the following does what I want .. if finds the Tuesday next week ...

              dtCutOffDate = DateTime.Now.Ad dDays(9 - (int)DateTime.N ow.DayOfWeek);

              Comment

              Working...