.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Calculating Date Difference

Posted By: jigs     Posted Date: March 02, 2011    Points:2   Category :ASP.Net
I have 3 field, start date, End date and Difference and all these field has datatype - timestamp.

Now, what I am looking for is when I select my start date from my date picker and End date is also from date picker then the result should populate in my date difference text box but its not comming...

Please, help as I am new to ASP.NET.

I am giving sample code

Dim cn As New OracleConnection("Data Source=abc; User Id=ab;Password=abcd")

Dim SQL As String
'build the INSERT statement

Dim xy As New System.Text.StringBuilder

'Dim txtHireDate As String = strDate.ToString("dd-MMM-yy hh:mm:ss tt").ToUpper()

Dim dtStartDate As DateTime = AddCurrentTime(DateTime.Parse(txtStartDate.Text))

Dim dtEndDate As DateTime = AddCurrentTime(DateTime.Parse(txtEndDate.Text))

Dim tsTimeSpan As TimeSpan

'= dtEndDate.Subtract(dtStartDate)

'txtOverlap.Text = ts.Days

'Dim dtOvxerlap As TimeSpan = dtEndDate.Subtract(dtStartDate)

'txtOverlap.Text = dtOverlap.Hours.ToString()

'Dim iNumberofDays As Integer
'Dim strMsgText As String

tsTimeSpan = dtEndDate.Subtract(dtStartDate)

txtOverlap.Text = tsTimeSpan.Days.ToString()
txtOverlap.Text = tsTimeSpan.Hours.ToString()
txtOverlap.Text = tsTimeSpan.Minutes.ToString()
txtOverlap.Text = tsTimeSpan.Seconds.ToString()

xy.Append(" INSERT INTO temp")
xy.Append(" (startdate, enddate, overlap)")
xy.Append(" VALUES")
xy.Append(" ('{0}','{1}','{2}')")
SQL = String.Format(xy.ToString, _
dtStartDate.ToString("dd-MMM-yy hh:mm:ss tt").ToUpper(), _
dtEndDate.ToString("dd-MMM-yy hh:mm:ss tt").ToUpper(), _
Me.txtOverlap.Text)



Responses
Author: Naresh             
Posted Date: March 02, 2011     Points: 20   

hi,

To find the difference between two dates are as follows,


Dim date1 As Date
Dim date2 As Date
date1 = Convert.ToDateTime(DateTimePicker1.Value)
date2 = Convert.ToDateTime(DateTimePicker2.Value)

Dim diff As System.TimeSpan
diff = date2 - date1
textbox1.text.Text = diff.Days & ":" & diff.Hours & ":" & diff.Minutes & ":" & diff.Seconds



Thanks
Naresh
Author: jigs             
Posted Date: March 02, 2011     Points: 20   

Hello,

Let me explain you what I am doing in my page.

I am trying to insert record in my table. For that I have start date, end date and date diff. I am selecting my start date, end date from my date picker. If I place my cursor in date diff text field then result should populate and then I can click on my insert button and record will enter into my database.

Please help ....I have make some changes to my code and getting error " ORA-01843: not a valid month "

Dim SQL As String
'build the INSERT statement
Dim xy As New System.Text.StringBuilder
'Dim strDate As Date

Dim dtStartDate As DateTime = AddCurrentTime(DateTime.Parse(txtStartDate.Text))

Dim dtEndDate As DateTime = AddCurrentTime(DateTime.Parse(txtEndDate.Text))

Dim ts As TimeSpan

'ts = dtEndDate.Subtract(dtStartDate)

ts = dtEndDate - dtStartDate

txtOverlap.Text = ts.Days & ":" & ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds

xy.Append(" INSERT INTO temp")
xy.Append(" (startdate, enddate, overlap)")
xy.Append(" VALUES")
xy.Append(" ('{0}','{1}','{2}')")
SQL = String.Format(xy.ToString, _
dtStartDate.ToString("dd-MMM-yy hh:mm:ss tt").ToUpper(), _
dtEndDate.ToString("dd-MMM-yy hh:mm:ss tt").ToUpper(), Me.txtOverlap.Text)


Author: Naresh             
Posted Date: March 02, 2011     Points: 20   

hi jigs,

u can write the datedifference coding in selectedindexchange property of datetimepicker2.

For the error, i think u date and month must have been changed, First u convert into ur reguire format and then insert to database,



Thanks
Naresh
Author: Maverick             
Posted Date: March 07, 2011     Points: 20   

Hello,
I think your date difference logic ok just need to write on the index changed event of ur datetime picker and as far as your date insert error is concerned u need to convert it into orwacle acceptable format.i.e dd-mm-yyyy.
07-03-2011.
Hope it helps.
Maverick

Thanks,
Maverick


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend