473,320 Members | 1,856 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,320 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 4752
"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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.