简体   繁体   English

如何使VBScript中的日期函数动态化,获取当前日期的前一个日期?

[英]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) 我的日期函数是: 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) 正如您所看到的, Day函数将获得前一天,因为它是负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 . 例如,今天是2016年8月1日。根据上面的日期函数,它将找到160831而不是160731 I am also worried since some months ends with 30 and 31 . 我也担心,因为有些月份以3031结尾。

Is there a way to get the previous date using VBScript dynamically? 有没有一种方法可以动态地使用VBScript获取以前的日期? 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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