Time and date from string to calculation

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello

I got 2 string that represent time as (yyyymmdd hh:mm:ss)

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:02"

Now, I need to calculate

(T2- T1) + T2

What is the step that I need to accomplish to get the result equal to
"20110922 19:03:00"

How do I transform a string date to a string number

Martin
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hello

I got 2 string that represent time as (yyyymmdd hh:mm:ss)

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:02"

Now, I need to calculate

(T2- T1) + T2

What is the step that I need to accomplish to get the result equal to
"20110922 19:03:00"

How do I transform a string date to a string number

Martin
Are you only interested in the TIME portion of the string for the calculation?
 
Upvote 0
Give this formula a try...

=(TEXT(LEFT(T2,8),"0000-00-00")&MID(T2,9,9))-(TEXT(LEFT(T1,8),"0000-00-00")&MID(T1,9,9))+(TEXT(LEFT(T2,8),"0000-00-00")&MID(T2,9,9))

and Custom Format the cell using this pattern...

yyyymmdd hh:mm:ss
 
Upvote 0
I'm in VBA

Here is the code that I wrote
Code:
Sub timedatecalculation()
Dim t1, t2 As String
Dim T1D, T2d, t3, t4 As Date

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:00"
T1D = DateValue(Val(Mid(t1, 1, 4)) & "," & Val(Mid(t1, 5, 2)) & "," & Val(Mid(t1, 7, 2))) & " " & (Mid(t1, 10, 8))
T2d = DateValue(Val(Mid(t2, 1, 4)) & "," & Val(Mid(t2, 5, 2)) & "," & Val(Mid(t2, 7, 2))) & " " & (Mid(t2, 10, 8))


t3 = T2d - T1D

End Sub

I got an incompatible value when i subtract T2d-T1D

Martin
 
Upvote 0
OK, Rick's suggestion does what you asked for but I have to wonder if that REALLY is what you want.




Consider this:

T1 = 20110922 18:57:00
T2 = 20110922 19:00:00

What result would you expect for that data?

I need (T2 -t1) + t2

The end string will have to be : "20110922 19:03:00"

Note: I modified the number of your post

These number represent, on a stock chart,
- The ending date & time of a preceding bar
- The ending date & time of the actual bar
- I want to get the date & time of the next trading bar ending

These number are formated for my broker server and they are read and sent like T1 & T2 format string

Martin
 
Upvote 0
Sorry, I had mistaken your T1 and T2 to be cell addresses for some reason. Consider doing what you want this way...

Code:
T1 = "20110922 18:57:00"
T2 = "20110922 19:00:00"
RealT1 = CDate(Format(T1, "@@@@-@@-@@ @@@@@@@@"))
RealT2 = CDate(Format(T2, "@@@@-@@-@@ @@@@@@@@"))
NewTime = Format((RealT2 - RealT1) + RealT2, "yyyymmdd hh:mm:ss")
 
Upvote 0
Resolved: Time and date from string to calculation

Hello

I did resolve my problem. Thanks to you guy

Code:
Sub timedatecalculation()
Dim t1, t2 As String
Dim T1D, T2d, t3, t4 As Date

t1 = "20110922 18:57:00"
t2 = "20110922 19:00:00"
T1D = DateValue(Val(Mid(t1, 1, 4)) & "," & Val(Mid(t1, 5, 2)) & "," & Val(Mid(t1, 7, 2))) + TimeValue((Mid(t1, 10, 8)))

T2d = DateValue(Val(Mid(t2, 1, 4)) & "," & Val(Mid(t2, 5, 2)) & "," & Val(Mid(t2, 7, 2))) + TimeValue((Mid(t2, 10, 8)))

t3 = T2d - T1D + T2d
t3 = Format(t3, "yyyymmdd hh:mm:ss")

End Sub

Martin :)
 
Upvote 0
Re: Resolved: Time and date from string to calculation

I did resolve my problem. Thanks to you guy

Code:
Sub timedatecalculation()
Dim t1, t2 As String
Dim T1D, T2d, t3, t4 As Date
 
t1 = "20110922 18:57:00"
t2 = "20110922 19:00:00"
T1D = DateValue(Val(Mid(t1, 1, 4)) & "," & Val(Mid(t1, 5, 2)) & "," & Val(Mid(t1, 7, 2))) + TimeValue((Mid(t1, 10, 8)))
 
T2d = DateValue(Val(Mid(t2, 1, 4)) & "," & Val(Mid(t2, 5, 2)) & "," & Val(Mid(t2, 7, 2))) + TimeValue((Mid(t2, 10, 8)))
 
t3 = T2d - T1D + T2d
t3 = Format(t3, "yyyymmdd hh:mm:ss")
 
End Sub
You might want to study my 3-Format-Statements solution... it is much more compact and, in my view, easier to read/maintain (once you understand the @ metacharacter usage that is).
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,629
Members
452,933
Latest member
patv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top