473,782 Members | 2,479 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 #1
12 1810
"andyoye" <an*****@nospam .comwrote in message
news:O4******** ******@TK2MSFTN GP05.phx.gbl...
>I have two date fields(startDat e, endDate) in a form and I want to
calculate number of days between startDate & endDate.
int days = ((TimeSpan)(end Date-startDate)).Day s;
Oct 2 '08 #2
Hi Andy,

DateDiff in VB is a little bit from pre history.

This it can be in VB 2008
\\\
Dim EndDate = DateTime.Now
Dim StartDate = Now.AddDays(-3)
Dim days = EndDate - StartDate
MessageBox.Show (days.Days.ToSt ring)
///

This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.Ad dDays(-3);
var days = EndDate - StartDate;
MessageBox.Show (days.Days.ToSt ring());
///

I real don't see much differences.

The VB code only in this case to show that the question is a little bit
strange.

Cor
"andyoye" <an*****@nospam .comschreef in bericht
news:O4******** ******@TK2MSFTN GP05.phx.gbl...
>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 #3
startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:um******** ******@TK2MSFTN GP05.phx.gbl...
Hi Andy,

DateDiff in VB is a little bit from pre history.

This it can be in VB 2008
\\\
Dim EndDate = DateTime.Now
Dim StartDate = Now.AddDays(-3)
Dim days = EndDate - StartDate
MessageBox.Show (days.Days.ToSt ring)
///

This it can be in C# 2008
\\\\
var EndDate = DateTime.Now;
var StartDate = DateTime.Now.Ad dDays(-3);
var days = EndDate - StartDate;
MessageBox.Show (days.Days.ToSt ring());
///

I real don't see much differences.

The VB code only in this case to show that the question is a little bit
strange.

Cor
"andyoye" <an*****@nospam .comschreef in bericht
news:O4******** ******@TK2MSFTN GP05.phx.gbl...
>>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 #4
"andyoye" <an*****@nospam .comwrote in message
news:ek******** ******@TK2MSFTN GP06.phx.gbl...
startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)
Alberto gave you the answer.
Oct 2 '08 #5
Below is smaple code from my form

{

1: XPathNavigator docFM = this.CreateNavi gator();

2: string date_form = docFM.SelectSin gleNode("/my:myFields/my:startDate",
this.NamespaceM anager).Value;

3: XPathNavigator docFN = this.CreateNavi gator();

4: string date_to = docFN.SelectSin gleNode("/my:myFields/my:startTime",
this.NamespaceM anager).Value;

5: DateTime startDate = Convert.ToDateT ime(date_frorm) ;

6: DateTime endDate = Convert.ToDateT ime(date_to);

7: TimeSpan dateDifference = endDate.Subtrac t(startDate);

8: int days = dateDifference. Days;

9: string daysString = days.ToString() ;

10: XPathNavigator dateDiffTextBox Value =
this.CreateNavi gator().SelectS ingleNode("/my:myFields/my:dateDiff",
this.NamespaceM anager);

11: dateDiffTextBox Value.SetValue( daysString);

}

I get
"String was not recognized as a valid DateTime." at line 6
"When convertin a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object"

thx

"Jeff Johnson" <i.***@enough.s pamwrote in message
news:ei******** ******@TK2MSFTN GP06.phx.gbl...
"andyoye" <an*****@nospam .comwrote in message
news:ek******** ******@TK2MSFTN GP06.phx.gbl...
>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.

Oct 2 '08 #6
Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't understand
me wrong, nothing wrong with Alberto its code, however you give the
impression there is something wrong with mine?

Cor

"Jeff Johnson" <i.***@enough.s pamschreef in bericht
news:ei******** ******@TK2MSFTN GP06.phx.gbl...
"andyoye" <an*****@nospam .comwrote in message
news:ek******** ******@TK2MSFTN GP06.phx.gbl...
>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.
Oct 2 '08 #7
I got it guys, I was passing a wrong var

thanks all.. you all rocks!!!
"andyoye" <an*****@nospam .comwrote in message
news:Op******** ******@TK2MSFTN GP06.phx.gbl...
Below is smaple code from my form

{

1: XPathNavigator docFM = this.CreateNavi gator();

2: string date_form = docFM.SelectSin gleNode("/my:myFields/my:startDate",
this.NamespaceM anager).Value;

3: XPathNavigator docFN = this.CreateNavi gator();

4: string date_to = docFN.SelectSin gleNode("/my:myFields/my:startTime",
this.NamespaceM anager).Value;

5: DateTime startDate = Convert.ToDateT ime(date_frorm) ;

6: DateTime endDate = Convert.ToDateT ime(date_to);

7: TimeSpan dateDifference = endDate.Subtrac t(startDate);

8: int days = dateDifference. Days;

9: string daysString = days.ToString() ;

10: XPathNavigator dateDiffTextBox Value =
this.CreateNavi gator().SelectS ingleNode("/my:myFields/my:dateDiff",
this.NamespaceM anager);

11: dateDiffTextBox Value.SetValue( daysString);

}

I get
"String was not recognized as a valid DateTime." at line 6
"When convertin a string to DateTime, parse the string to take the date
before putting each variable into the DateTime object"

thx

"Jeff Johnson" <i.***@enough.s pamwrote in message
news:ei******** ******@TK2MSFTN GP06.phx.gbl...
>"andyoye" <an*****@nospam .comwrote in message
news:ek******* *******@TK2MSFT NGP06.phx.gbl.. .
>>startDate field value is 10-01-09
endDate field value is 10-05-09

How can I get 4 .....(endDate-startDate)

Alberto gave you the answer.


Oct 2 '08 #8
"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:C4******** *************** ***********@mic rosoft.com...
Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't
understand me wrong, nothing wrong with Alberto its code, however you give
the impression there is something wrong with mine?
Wait, are you asking me why I responded the way I did? If so, I was simply
telling andyoye that he was asking for an answer yet again when Alberto had
already given him the answer. In other words, "Pay attention: you've already
been told!"
Oct 2 '08 #9
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.

"Jeff Johnson" <i.***@enough.s pamwrote in message
news:eg******** ******@TK2MSFTN GP04.phx.gbl...
"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:C4******** *************** ***********@mic rosoft.com...
>Jeff,

Because this is in my answering thread.

I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
code in your idea the difference between those two answers. Don't
understand me wrong, nothing wrong with Alberto its code, however you
give the impression there is something wrong with mine?

Wait, are you asking me why I responded the way I did? If so, I was simply
telling andyoye that he was asking for an answer yet again when Alberto
had already given him the answer. In other words, "Pay attention: you've
already been told!"

Oct 3 '08 #10

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

Similar topics

2
7157
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
16517
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
8008
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
11917
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
4984
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
6690
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
15505
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
9480
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
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7494
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
6735
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
5378
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.