473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get the date?

Hi Experts:

Using C#, knowing today's date, how to get the date of NEXT Tuesday?

For example, if today is 2007-04-26, I'd like to get the date of next
Tuesday which is 2007-05-01.

Thanks in advance!
Polaris
May 29 '07 #1
5 6114
>Using C#, knowing today's date, how to get the date of NEXT Tuesday?
>
For example, if today is 2007-04-26, I'd like to get the date of next
Tuesday which is 2007-05-01.
Try something like this

DateTime dt = DateTime.Now;
int daysToAdd;

if (dt.DayOfWeek == DayOfWeek.Tuesday)
daysToAdd = 7;
else
daysToAdd = ((int)DayOfWeek.Tuesday - (int)dt.DayOfWeek + 7) % 7;

Console.WriteLine(dt.AddDays(daysToAdd));
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 29 '07 #2
Hi Polaris,

E.g. dateTime.AddDays(9 - (int)dateTime.DayOfWeek);

Regards,
Alex
http://devkids.blogspot.com
Hi Experts:

Using C#, knowing today's date, how to get the date of NEXT Tuesday?

For example, if today is 2007-04-26, I'd like to get the date of next
Tuesday which is 2007-05-01.

Thanks in advance!
Polaris

May 29 '07 #3

"Polaris" <et*******@hotmail.comwrote in message
news:uO**************@TK2MSFTNGP02.phx.gbl...
Hi Experts:

Using C#, knowing today's date, how to get the date of NEXT Tuesday?

For example, if today is 2007-04-26, I'd like to get the date of next
Tuesday which is 2007-05-01.
date = date.AddDays(1.0);

if (date.DayOfWeek == DayOfWeek.Saturday)
do something;
And previous date:

date = date.AddDays(-1.0);
if (date.DayOfWeek == DayOfWeek.Saturday) do something;

May 29 '07 #4

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:OW**************@TK2MSFTNGP06.phx.gbl...
Using C#, knowing today's date, how to get the date of NEXT Tuesday?

For example, if today is 2007-04-26, I'd like to get the date of next
Tuesday which is 2007-05-01.

Try something like this

DateTime dt = DateTime.Now;
int daysToAdd;

if (dt.DayOfWeek == DayOfWeek.Tuesday)
daysToAdd = 7;
else
daysToAdd = ((int)DayOfWeek.Tuesday - (int)dt.DayOfWeek + 7) % 7;
or just
daysToAdd = ((int)DayOfWeek.Tuesday - (int)dt.DayOfWeek + 6) % 7 + 1;

with no need to handle Tuesday separately.
>
Console.WriteLine(dt.AddDays(daysToAdd));
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 29 '07 #5
>DateTime dt = DateTime.Now;
>int daysToAdd;

if (dt.DayOfWeek == DayOfWeek.Tuesday)
daysToAdd = 7;
else
daysToAdd = ((int)DayOfWeek.Tuesday - (int)dt.DayOfWeek + 7) % 7;

or just
daysToAdd = ((int)DayOfWeek.Tuesday - (int)dt.DayOfWeek + 6) % 7 + 1;

with no need to handle Tuesday separately.

Thanks, I figured there was a way to get rid of the special case, but
it was too early in the morning for me to think of it :)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
3
by: captain | last post by:
Below is the sql for data with same date need to extract + or - 5 days data of same date also. How to also get data of + and - days related to same date. SELECT IM.Area, IM.Location,...
1
by: Liz Malcolm | last post by:
Hello and TIA. I have a DE form with an option group that if daily is selected todays date is used for start and end date, if weekly is selected Monday - Friday is used. I am trying to add a...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
by: JJ | last post by:
Here's the code. $link="http://xbox360cheat.org"; $close_date=$_POST; #last content change check if ($close_date == 0) $close_date = date("Y-m-d H:m:s", mktime(12, 0, 0, date("m"), date...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.