简体   繁体   中英

How to make date function in VBScript dynamic, getting the previous date of the current date?

I have this code here that will copy files based on the filename, by filename means getting the previous date of the current date, but my problem here is the first day of the month.

FSO.CopyFile "D:\Input\rep_*" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2) & ".txt", "D:\Output\"

My date function is: Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now - 1), 2)

And as you can see, the Day function will get the previous day since it is minus 1. Day(Now - 1)

My problem is the first day of the month. For example, today is August 1, 2016. and based on the date function above, it will find 160831 not 160731 . I am also worried since some months ends with 30 and 31 .

Is there a way to get the previous date using VBScript dynamically? Thanks in advance.

If you want yesterday, take one day off the actual date before formatting it, something like this:

First find yesterday:

y = DateAdd("d",-1,Date())

Now format it:

y = DateAdd("d",1,Date())
z = Year(y) & Right("0" & Month(y), 2) & Right("0" & Day(y), 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM