0

I need format the date of the previous day in this format, with VBScript :

yyyymmdd

And I have tried this solution :

NewData = Right(Year(DateSerial(Year(Date()),Month(Date()),1)),4) &_
          Right(String(2, "0") &_
          Month(DateSerial(Year(Date()),Month(Date()),1)), 2) &_
          Right(String(2, "0") &_
          Day(DateAdd("d",-1, Now())), 2)  

But instead of getting :

20190630

I have :

20190730

Can you help me ?

Thanks in advance for any help.

  • Not sure if this should be closed as a duplicate. OP wasn't having issues with the formatting part, but only with the "previous day" part. – Geert Bellekens Jul 01 '19 at 14:05

1 Answers1

1

You should first store yesterday in a variable and then do your formatting magic on this date.

dim yesterday
yesterday = DateAdd("d",-1, Now())
NewData = Right(Year(DateSerial(Year(yesterday),Month(yesterday),1)),4) _
        & Right(String(2, "0") _
        & Month(DateSerial(Year(yesterday),Month(yesterday),1)), 2) _
        & Right(String(2, "0") & Day(yesterday), 2) 

I strongly suspect there are more straightforward ways to get a date in format YYYYMMDD however.

Cid
  • 14,968
  • 4
  • 30
  • 45
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50