# Format date and time - 0001

Hello all,

In my excel .Cells(i, 11), i have a date and time format "201008160100" for example. Year:2010, month:08, Day:10, Hour:01, Min:00.

I need to have in cell 13 the value from .Cells(i, 11) but remove 1 minute.

The result of the example "201008160100" would be "201008160059"

.Cells(i, 13) = .Cells(i, 11) - "0001"
LVL 11
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
=TEXT(DATE(LEFT(A1,4),MID(A1,5,2),MID(A1,7,2))+TIME(MID(A1,9,2),MID(A1,11,2),0)-TIME(0,1,0),"yyyymmddhhmm")
Commented:
Try this function....

Function subtractMinute(dateString As String) As String
Dim timeString As String
timeString = Right(dateString, 4)

Dim timeVal As Date
timeVal = TimeSerial(Left(timeString, 2), Right(timeString, 2), 0)

Dim newDateString As String
newDateString = Left(dateString, 8) & Format(DateAdd("n", -1, timeVal), "hhmm")

subtractMinute = newDateString
End Function

Use it like this....

MsgBox subtractMinute("201008160100")

Wayne
Commented:
For that matter, you can use it as a worksheet formula as well.

Wayne
Author Commented:
Hello,

I have a syntax error:
.Cells(i, 13) = TEXT(DATE(LEFT(A1,4),MID(A1,5,2),MID(A1,7,2))+TIME(MID(A1,9,2),MID(A1,11,2),0)-TIME(0,1,0),"yyyymmddhhmm")
Commented:
s = .Cells(i, 11)
s = Format(DateAdd("n", -1, DateSerial(Left(s, 4), Mid(s, 5, 2), Mid(s, 7, 2)) + TimeSerial(Mid(s, 9, 2), Mid(s, 11, 2), 0)), "yyyymmddhhmm")
.Cells(i, 13) = s

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
webtubbs - would your code handle taking 1 minute off 0000? first day of year?
Commented:
Just as an alternative:
=TEXT(TEXT(A1,"0000""-""00""-""00 00"":""00")*1-TIME(0,0,1),"yyyymmddhhmm")
Mechanical EngineerCommented:
You can shorten Rory's formula using the backslash to indicate literals:
=TEXT(TEXT(A1,"0000\-00\-00 00\:00")-1/1440,"yyyymmddhhmm")
Author Commented:
Thanks to everybody.

Now it works.

Thanks again :)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.