0

I am working on editing a .VBS code. I want to have edit a date stamp in the file name.

It used to be:

dateStamp = CStr((Year(reportDate) * 10000) + (Month(reportDate) * 100) + Day(reportDate))

so: YYYYMMDD

and I wanted to have something like YYYY-MM-DD:

dateStamp = CStr(Year(reportDate)) & "-" & CStr(Month(reportDate)) & "-" & CStr(Day(reportDate))

but this code gives me YYYY-M-DD

Something like:

dateStamp = CStr(Year(reportDate)) & "-" & CStr(Right(String(2, "0") & Month(reportDate), 2)) & "-" & CStr(Day(reportDate))

doesn't work.

Could you help me with it?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mateusz Konopelski
  • 1,000
  • 4
  • 20
  • 37
  • @Ansgar-Wiechers Is actually more likely due to the incorrect quote usage so the close reason should probably be *"Questions about a problem that can no longer be reproduced or that was caused by a simple typographical error. While similar questions may be on-topic here, these are often resolved in a manner unlikely to help future readers."* either way it should be closed. – user692942 Jan 21 '16 at 12:16

1 Answers1

0

This should work, assuming "reportDate" is a validate date/time string:

Year(reportDate) & "-" &  _
Right("0" & Month(reportDate),2) & "-" & _
Right("0" & Day(reportDate),2)

If you substitute in Now for reportDate you can show it working, if there is any issue with the existing variable.

EDIT:

BTW, yours would have worked, though it is more involved than it needs to be. You just needed to correct the "magic quotes" from the Month section of the code. The quotes around the zero are not correct double quotes.

CStr(Right(String(2, “0”) & Month(reportDate), 2))

But you don't need the extra function calls. I'd use the one from my example line up above.

RLH
  • 1,545
  • 11
  • 12