473,395 Members | 2,423 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,395 software developers and data experts.

Is their a method in TimeSpan or Time function that...

Hi,

How can I return the Total days between 2 dates but have it only sum the
WeekDays and not Weekends. for example Lets say: How many days have pasted
from August 4(Monday) to August 10(Sunday) should return 5 days and not 7.

How can this be done? Is their a method to do it... Any ideas are
appreciated,

Thanks Ahead
Lucas
Jul 19 '05 #1
2 4758
"Lucas Sain" <ls***@lidersoft.com> wrote
How can I return the Total days between 2 dates but have it only sum the
WeekDays and not Weekends. for example Lets say: How many days have pasted
from August 4(Monday) to August 10(Sunday) should return 5 days and not 7.


Here is one way....

Given the following function:

Private Function GetNonWeekendDayTotal(ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Integer

Dim totalWeekDays As Integer = 0

For i As Integer = 1 To EndDate.Subtract(StartDate).Days
If StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Saturday And _
StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Sunday Then

totalWeekDays += 1
End If
Next

Return totalWeekDays
End Function

Call it as follows:

Dim i As Integer = GetNonWeekendDayTotal( _
New DateTime(2002, 8, 4, New GregorianCalendar(GregorianCalendarTypes.USEnglish )), _
New DateTime(2002, 8, 10, New GregorianCalendar(GregorianCalendarTypes.USEnglish )))

For the GregorialCalendar class, you'll need to the following import:

Imports System.Globalization

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com


Jul 19 '05 #2
Carl,

Thanks, look like a solution.

Regards
Lucas
"Carl Prothman [MVP]" <ca****@spamcop.net> wrote in message
news:ef**************@TK2MSFTNGP09.phx.gbl...
"Lucas Sain" <ls***@lidersoft.com> wrote
How can I return the Total days between 2 dates but have it only sum the
WeekDays and not Weekends. for example Lets say: How many days have pasted from August 4(Monday) to August 10(Sunday) should return 5 days and not 7.
Here is one way....

Given the following function:

Private Function GetNonWeekendDayTotal(ByVal StartDate As DateTime,

ByVal EndDate As DateTime) As Integer
Dim totalWeekDays As Integer = 0

For i As Integer = 1 To EndDate.Subtract(StartDate).Days
If StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Saturday And _ StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Sunday Then

totalWeekDays += 1
End If
Next

Return totalWeekDays
End Function

Call it as follows:

Dim i As Integer = GetNonWeekendDayTotal( _
New DateTime(2002, 8, 4, New GregorianCalendar(GregorianCalendarTypes.USEnglish )), _ New DateTime(2002, 8, 10, New GregorianCalendar(GregorianCalendarTypes.USEnglish )))
For the GregorialCalendar class, you'll need to the following import:

Imports System.Globalization

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com

Jul 19 '05 #3

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

Similar topics

1
by: Doug | last post by:
Is it possible there is a bug with the System.TimeSpan object in .NET? I am using it to track times and am seeing an unusual situation. From what I understand, there are 1000 milliseconds in a...
2
by: Philip Townsend | last post by:
I need to determine if a file's last write time is greater than 15 minutes. Is there a simple way to do this with the TimeSpan object? Thanks! *** Sent via Developersdex...
6
by: Millo | last post by:
Hi I'm trying to call the method Contains in a loop: out of the loop I set the primary key of the dataTable. The method returns true one time at a certani value, but then, when the loop steps...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
2
by: ucasesoftware | last post by:
i translate a C# funtion to VB.NET and i have this : Shared Function isAllDay(ByVal ap As Appointment) As Boolean Return ap.DateBegin.TimeOfDay = TimeSpan.Zero AndAlso ap.DateEnd.TimeOfDay =...
5
by: gh | last post by:
I have 2 columns in a grid where the user enters a begining time(TI) and at the end of the day an end time(TO). I then want to take the TO - TI to get the difference and display it as hours and...
4
by: Peter Proost | last post by:
Hi group, it's been a long time since the last time I've been here but I have a question. I'm working with timespan.parse for calculating a duration, I have to add strings which are in the...
2
by: =?Utf-8?B?WVhR?= | last post by:
Hello, I want to convert the timespan (1.12:25:23) to 1day 12hour 25min 23sec, how to do it? thank you.
7
by: hatch | last post by:
The project I am working on has to have start and stop buttons on a form and a label to show the eslaped time once the start button is pushed. The elapsed tome should stop when the stop button is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.