473,791 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use VB DateDiff function in C#

I have two date fields(startDat e, endDate) in a form and I want to
calculate number of days between startDate & endDate.

Thanks
Oct 2 '08
12 1811
"andyoye" <an*****@nospam .comwrote in message
news:OZ******** ******@TK2MSFTN GP05.phx.gbl...
>I already said: You ALL Rocks, thanks.

How about getting minutes difference? I mod the days code

TimeSpan timeDifference = endTime.Subtrac t(startTime);

int hours = timeDifference. Hours;

string hoursString = hours.ToString( );

Now I do get hours but not minutes.
Okay, what is the ULTIMATE result you're looking for? In other words, given
two DateTime values, what would you like to see as the result of the
difference between them?
Oct 3 '08 #11
how about time difference to the minutes? below date and time values are
entered by users in text fields.

So startDate = 10-01-2008 startEnd= 2:05 PM
endDate = 10-03-2008 endTime = 1:00 PM

result should be 1days 22 hrs 55mins
"Jeff Johnson" <i.***@enough.s pamwrote in message
news:Ox******** ******@TK2MSFTN GP05.phx.gbl...
"andyoye" <an*****@nospam .comwrote in message
news:OZ******** ******@TK2MSFTN GP05.phx.gbl...
>>I already said: You ALL Rocks, thanks.

How about getting minutes difference? I mod the days code

TimeSpan timeDifference = endTime.Subtrac t(startTime);

int hours = timeDifference. Hours;

string hoursString = hours.ToString( );

Now I do get hours but not minutes.

Okay, what is the ULTIMATE result you're looking for? In other words,
given two DateTime values, what would you like to see as the result of the
difference between them?

Oct 6 '08 #12
"andyoye" <an*****@nospam .comwrote in message
news:eD******** ******@TK2MSFTN GP03.phx.gbl...
how about time difference to the minutes? below date and time values are
entered by users in text fields.

So startDate = 10-01-2008 startEnd= 2:05 PM
endDate = 10-03-2008 endTime = 1:00 PM

result should be 1days 22 hrs 55mins
[Sorry for the late reply; I forgot I started following this group!]

The best thing to do is to get the difference between the dates in the
smallest possible unit you're looking for (minutes in this case) and then
apply math to extract the larger units. For example, you've got 2815 minutes
there. You make some constants like

private const int MINUTES_PER_DAY = 1440
private const int MINUTES_PER_HOU R = 60 // Yes, this one might be
overkill....

and then you test your value against them. If you have more than the
constant, then you've got at least one of that unit, so you do some integer
division to get the number of units and then you test the remainder against
the next smaller unit.

Instead of constants, you could make a class which holds the number and a
description (1440 / "days", 60 / "hrs", etc.) and then make an array or
collection of objects of that class so you could process them in a loop.
Then you could make different arrays/collections for different units
(seconds, milliseconds, etc.).
Oct 13 '08 #13

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

Similar topics

2
7158
by: News Central | last post by:
To all! I use the DateDiff function using VB6 and get this error 'Wrong number of argument or invalid property assignment' ... have anyone seen this problem? thanks ....
7
1915
by: Drago | last post by:
Hi, I got the next question, I want to know the diference between two dates and thats is really easy, but my problem is get that diference in the following format ex. "the diference is= 0 year, 9 months, 11 days" var1 = 04/04/1999 var2 = 01/11/2000 Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days
6
16519
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days between values in the database and the current date. "No problem," thought I , "I'll just use the SQL DATEDIFF command." Heh! Well, the user interface I'm using didn't even recognise DATEDIFF as being a function, so I decided to visit the mySQL...
1
8009
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days until completion: DateDiff("d",,) is TODAY the appropriate syntax for today's date? Could the function/expression even be set up that way, or do I need to do something else to get 'days until completion'? (My Access manuals do not mention a TODAY...
4
11918
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS STARTINGDATE1 ENDINGDATE1 STARTINGDATE2
1
4985
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a blank. I've tried; > >DateDiff("y",-4,DateIn) and get errors > >Please any assistance would be greatly appreciated. >
5
6692
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than Access. I have a database where they customer wants to purge records from certain tables after three years. When I was asked to make the changes I originally thought this was an incredibly simple thing to do. I looked at the function list and...
7
15506
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
6
7650
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
2779
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error like datediff(mm,date1,date2) datediff(mmm,date1,date2) datediff(month,date1,date2) datediff("mm",date1,date2) datediff("mmm",date1,date2) datediff("month",date1,date2)
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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 we have to send another system

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.