Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding time from 1 datetime to another

Status
Not open for further replies.

mushin

Programmer
Oct 4, 2000
49
Every time I think I understand datetime arithmetic, I prove myself wrong ......

I have 2 datetimepickers, one for date selection and 1 for
time selection.

My DB has 1 datetime field.

My user selects a date then a start time in the 2 DTP's

I want to combine the time portion from time dtp to the
date portion of the date dtp.


Code:
' dtwork vars are all datetime

dtWork = DTPstartTime.Value ' grab time portion of start time field
dtWork2 = DTPstart.Value ' set work var to apptdate

' now add time potion from start time to new dt

'================================================
dtWork2 = dtWork.AddHours(dtWork.Hour).AddMinutes(dtWork.Minute)

'==================================================

MessageBox.Show("Calc datetime", dtWork2.ToString)

This works, but doubles the time added. If I add 1 minute, the calc adds 2 and so on....

Is there a better way to do this ? Even If I get this to work right, seems like a lot of work for this.....

 
Code:
 dtWork2 = dtWork.AddHours(dtWork.Hour).AddMinutes(dtWork.Minute)

You're adding the hours & minutes back to itself. Try:

[/code]
dtWork2.AddMinutes(dtWork.Minute)
dtWork2.AddHours(dtWork.Hour)
[/code]

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top