Format the date of the previous day format yyyymmdd with VBScript [duplicate]

限于喜欢 提交于 2019-12-11 15:13:46

问题


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.


回答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.



来源:https://stackoverflow.com/questions/56835846/format-the-date-of-the-previous-day-format-yyyymmdd-with-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!