473,473 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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#?

Expand|Select|Wrap|Line Numbers
  1. function getCutOffDate ($tDate) {
  2.     $cDays = (int) date("w",strtotime($tDate));
  3.     return strtotime($tDate." -".($cDays + 5)." days");
  4. }
  5.  
Oct 14 '10 #1
6 3784
hype261
207 New Member
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static DateTime getCutOffDate(DateTime date)
  3. {
  4. //figure out which day of the week we are on
  5.  
  6. //figure out difference between that day and last Tuesday
  7.  
  8. //use the DateAdd function to compute our new date
  9. }
  10.  
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.
Oct 14 '10 #2
Curtis Rutland
3,256 Recognized Expert Specialist
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:

Expand|Select|Wrap|Line Numbers
  1. private DateTime GetCutOffDate(DateTime date)
  2. {
  3.     int days = (int)date.DayOfWeek + 5;
  4.     return date.Subtract(TimeSpan.FromDays(days));
  5. }
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).
Oct 14 '10 #3
hype261
207 New Member
I was thinking something more like this myself..

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static DateTime GetCutOffDate(DateTime date)
  3. {
  4.    return date.AddDay(-1 * ((int(date.DayOfWeek)+5)));
  5. }
  6.  
Oct 14 '10 #4
Curtis Rutland
3,256 Recognized Expert Specialist
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.
Oct 14 '10 #5
Thanks for the feedback. I will look into it.
Oct 15 '10 #6
I found the following does what I want .. if finds the Tuesday next week ...

dtCutOffDate = DateTime.Now.AddDays(9 - (int)DateTime.Now.DayOfWeek);
Oct 30 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Matt | last post by:
Hello. I have an Informix SQL statement that I need to run in MS SQL Server. When I try to execute it I get the following error message from Query Analyzer: Server: Msg 195, Level 15, State 10,...
8
by: Dennis M. Marks | last post by:
What is the maximum valid date range for the date(yyyy,mm,dd) function. -- Dennis M. Marks http://www.dcs-chico.com/~denmarks/ Replace domain.invalid with dcsi.net -----= Posted via...
0
by: sumeet | last post by:
how do i convert a yyyymmdd format to yyyy,mm,dd format. also i want to know how do i get the number of days between two dates which r in the format yyyymmdd. how to convert date into unix time...
6
by: bryan.seaton | last post by:
I have a delete statement that is not doing what I want it to do: Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21 DAYS); It is supposed to delete all records that are 21...
4
by: Christine | last post by:
I am having the strangest problem. I use the Date function in several of my forms and modules in an Access 2000 mdb. Lately, wherever in my code (in this one mdb) I use the Date function, it...
3
by: galsaba | last post by:
What would be the function to convert date to the number of date for example, 2/14/05 is 45, becuse this is the 45th day of the year. What would be the function? Where can I find info on the web...
3
by: sparkle | last post by:
Hi, Does anybody know how to convert date to ticks from a dateTimePicker? What I'm using now isn't working. I'm trying to use a dateTimePicker to set an appointment in Outlook with a...
1
by: Ryan Ramsey | last post by:
I am trying to convert a value returned from the date() function in php 5.0 to a format .NET can use. DateTime dt_now = DateTime.Now; DateTime dt_last = new DateTime(Convert.ToInt32(dkpLast));...
3
by: David | last post by:
Hi, I have a form which is posting a user entered date to an asp page. The date is then used in a SQL string. The format of the date is received as dd/mm/yyyy, with the user typing the '/'...
3
by: murch.alexander | last post by:
I made a simple public function to set and return a date value (see below). I have a number of queries that call up the function to get the "As Of Date," which is typically set to today's date....
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.