Replacing text (date time string entries)

O

Odie

I have several documents with several text lines beginning with date time
string entries like:

20000101000000.375 blah blah
20000101000000.888 blah blah
20000101000001.346 blah blah
20000101000001.789 blah blah
20000101000002.104 blah blah
etc

The format is currently YYYYMMDDHHMMSS.SSS
I want to loop through each line and convert it YYYYMMDDSSSSS.SS

In other words replace each hours mins and seconds into total seconds.

Can anyone provide some suggestions?

TIA
 
J

Jay Freedman

I have several documents with several text lines beginning with date time
string entries like:

20000101000000.375 blah blah
20000101000000.888 blah blah
20000101000001.346 blah blah
20000101000001.789 blah blah
20000101000002.104 blah blah
etc

The format is currently YYYYMMDDHHMMSS.SSS
I want to loop through each line and convert it YYYYMMDDSSSSS.SS

In other words replace each hours mins and seconds into total seconds.

Can anyone provide some suggestions?

TIA

Like this:

Dim oRg As Range
Dim strTime As String
Dim lngSecs As Long

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Text = "[0-9]{14}." ' 14 digits and a period

Do While .Execute
' skip the date part
oRg.MoveStart unit:=wdCharacter, Count:=8
' strip off the period
strTime = Left$(oRg.Text, 6)
' compute time in seconds
lngSecs = CLng(Right$(strTime, 2))
lngSecs = lngSecs + 60 * CLng(Mid$(strTime, 3, 2))
lngSecs = lngSecs + 3600 * CLng(Left$(strTime, 2))
' convert back to a 6-digit string plus period
oRg.Text = Format(lngSecs, "000000.")
' prepare for next find
oRg.Collapse wdCollapseEnd
Loop
End With
 
O

Odie

Works perfectly Jay!! You rock -Thanks!!!! ;)



Jay Freedman said:
I have several documents with several text lines beginning with date time
string entries like:

20000101000000.375 blah blah
20000101000000.888 blah blah
20000101000001.346 blah blah
20000101000001.789 blah blah
20000101000002.104 blah blah
etc

The format is currently YYYYMMDDHHMMSS.SSS
I want to loop through each line and convert it YYYYMMDDSSSSS.SS

In other words replace each hours mins and seconds into total seconds.

Can anyone provide some suggestions?

TIA

Like this:

Dim oRg As Range
Dim strTime As String
Dim lngSecs As Long

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Text = "[0-9]{14}." ' 14 digits and a period

Do While .Execute
' skip the date part
oRg.MoveStart unit:=wdCharacter, Count:=8
' strip off the period
strTime = Left$(oRg.Text, 6)
' compute time in seconds
lngSecs = CLng(Right$(strTime, 2))
lngSecs = lngSecs + 60 * CLng(Mid$(strTime, 3, 2))
lngSecs = lngSecs + 3600 * CLng(Left$(strTime, 2))
' convert back to a 6-digit string plus period
oRg.Text = Format(lngSecs, "000000.")
' prepare for next find
oRg.Collapse wdCollapseEnd
Loop
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top