473,396 Members | 1,971 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,396 software developers and data experts.

Getting values from DateTime

I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
8 1505
Hi,

Use the calendar control in this way you dont have to worry about the
conversion, if you use something else like a textbox you should convert the
TextBox.Text to a DateTime before any date related operation
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"John Slate" <ph*******@yahoo.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Yes, but what I wanted to know was whether there was a method or
property which would return the number of weekdays, excluding weekends,
from the selected dates...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
The way you would find that out would be to review the properties and
methods of DateTime in the help file.

--Bob

"John Slate" <ph*******@yahoo.com> wrote in message
news:ua*************@TK2MSFTNGP15.phx.gbl...
Yes, but what I wanted to know was whether there was a method or
property which would return the number of weekdays, excluding weekends,
from the selected dates...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
DateTime dt_new;
DateTime dt_start, dt_end;
dt_start = Convert.ToDateTime("10/1/2004");
dt_end = Convert.ToDateTime("10/20/2004");

TimeSpan ts = dt_end - dt_start;
int numberOfDays = ts.Days;

for(int i = 0; i<numberOfDays; i++)
{
dt_new = dt_start.AddDays(i);
if(dt_new.DayOfWeek.ToString()!="Saturday"&&dt_new .DayOfWeek.ToString()!="Sunday")
{
listBox1.Items.Add(dt_new.ToString());
}
}

This will just look through every date between the two dates specified and see whether or not the date is saturday or sunday. If the date is, than it will not put that date in a listbox. I think this is what you are looking for.
I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class


User submitted from AEWNET (http://www.aewnet.com/)
Nov 16 '05 #5
there's one in every crowd

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
thanks!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7
It might be better to replace
if(dt_new.DayOfWeek.ToString()!="Saturday"&&dt_new .DayOfWeek.ToString()!="Sunday")
by
if(dt_new.DayOfWeek != DayOfWeek.Saturday && dt_new.DayOfWeek !=
DayOfWeek.Sunday)
because (a) it should be slightly faster, and (b) it doesn't rely on
locale-specific (if that's the right term) day names.

An even faster method could be to work out the number of complete weeks
(basically divide numberOfDays by 7) and multiply that by 5, then do some
adjustments for the partial weeks at the start and end of the date range -
but unless you're dealing with very long time spans it's probably not worth
the effort!

Chris Jobson

"alan.washington" <alan.washington@aew_nospam.com> wrote in message
news:uz**************@TK2MSFTNGP15.phx.gbl...
DateTime dt_new;
DateTime dt_start, dt_end;
dt_start = Convert.ToDateTime("10/1/2004");
dt_end = Convert.ToDateTime("10/20/2004");

TimeSpan ts = dt_end - dt_start;
int numberOfDays = ts.Days;

for(int i = 0; i<numberOfDays; i++)
{
dt_new = dt_start.AddDays(i);
if(dt_new.DayOfWeek.ToString()!="Saturday"&&dt_new .DayOfWeek.ToString()!="Sunday")
{
listBox1.Items.Add(dt_new.ToString());
}
}

This will just look through every date between the two dates specified and
see whether or not the date is saturday or sunday. If the date is, than
it will not put that date in a listbox. I think this is what you are
looking for.
I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class?


User submitted from AEWNET (http://www.aewnet.com/)

Nov 16 '05 #8
Liz
Hey
Try this ... it worked for me.
<code>
public string DateAdder(string Date,double DayCount)
{
string [] strDate = Date.Split('/');
int Day = Convert.ToInt32(strDate[0]);
int Month = Convert.ToInt32(strDate[1]);
int Year = Convert.ToInt32(strDate[2]);
double one = 1;
double two = 2;
DateTime dt = new DateTime(Year,Month,Day);
DateTime NewDt = dt.AddDays(DayCount);
DateTime FinalDate;
switch(NewDt.DayOfWeek)
{
case DayOfWeek.Saturday:
FinalDate = NewDt.AddDays(two);
break;
case DayOfWeek.Sunday:
FinalDate = NewDt.AddDays(one);
break;
default:
FinalDate = NewDt;
break;
}
string ReturnDate = FinalDate.Day + "/" + FinalDate.Month + "/" +
FinalDate.Year;
return ReturnDate;
</code>

Cheers
Liz

"John Slate" wrote:
I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #9

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

Similar topics

6
by: Michael Riggio | last post by:
Does anyone know a way to get system ticks in .Net? I found DateTime.Ticks, but that is equal to 100 nanoseconds. The reason I'm asking is because we're considering passing some sort of timestamp...
1
by: Matik | last post by:
Hello everybody, ------------------------------------------------- CREATE TABLE ( (18, 0) IDENTITY (1, 1) NOT NULL , NOT NULL, (10) NOT NULL , (10) NULL , CONSTRAINT PRIMARY KEY ...
1
by: rerdavies | last post by:
OS: WIndows Server 2003. Currently logged in user is running with German(German) regional settings. Code fragment: System.Globalization.CultureInfo culture = new...
4
by: Anon | last post by:
Hello All! I have written a simple app that auto downloads a file from a secure ftp, renames it, and moves it to a network location. Everything works great except the renaming part. I parse out...
4
by: planetthoughtful | last post by:
Hi All, I have a relatively simple web user control I've included in a page that simply presents 3 drop down lists and a submit button, that I use as a date picker (ie one list for day, one for...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
4
by: preeti13 | last post by:
Hi friends i have a probelm i am try to pass the value to the employeeid parameter but getting th error please help me how i can do this i am getting the error here is my code using System;...
2
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into...
0
by: sumansms | last post by:
Hi All, I am getting wrong date when I am using DateTime.Parse function. The following is the code.. DateValue = DateTime.Parse(datarow.ToString()).ToString("MM/dd/yyyy"); I am taking the data...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...
0
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,...

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.