简体   繁体   English

获取当前日期和上一个日期之间的差异

[英]Get the difference between current date and a previous date

If I enter echo %date% in cmd I will get the date it is today.如果我在 cmd 中输入echo %date%我会得到今天的日期。 Let us say that today is Wednesday, 21/11/2018 .假设今天是Wednesday, 21/11/2018 If I type echo %date% cmd will print Wed 11/21/2018 .如果我输入echo %date% cmd 将打印Wed 11/21/2018 Yesterday, it would have returned Tue 11/20/2018 .昨天,它会在 2018 年 11 月 20 日Tue 11/20/2018返回。 The difference between these two dates (in days) is 1 .这两个日期(以天为单位)之间的差异是1

Is there any way I can do this kind of subtraction in command prompt?有什么办法可以在命令提示符下进行这种减法吗? Something like:就像是:

set previous_date=Tue 11/20/2018
echo %date% - %previous_date%

I know this should be very easy to do in a programming language like python or VBScript , but I don't want to use any of those, unless it is absolutely necessary.我知道用pythonVBScript这样的编程语言应该很容易做到这一点,但我不想使用其中任何一个,除非绝对必要。 I can download and use custom command line tools, but I want to do this with a batch file.我可以下载和使用自定义命令行工具,但我想使用批处理文件来执行此操作。

Anyone who knows how?谁知道怎么做?

In one of your comments you say:在您的一条评论中,您说:

I have a bat file in my Startup folder that runs a process once a week.我的 Startup 文件夹中有一个 bat 文件,该文件每周运行一次。 It does this by checking the current day and running the process if its monday if %date:~0,1%==M ().它通过检查当前日期并在星期一如果 %date:~0,1%==M () 时运行进程来做到这一点。 Problem is I need this process to launch AT LEAST once every 7 days.问题是我需要这个过程至少每 7 天启动一次。 But if say I forget to open my laptop on a particular monday this it doesn't launch the process until the next monday.但是,如果说我忘记在特定的星期一打开我的笔记本电脑,它直到下一个星期一才会启动该过程。 So now I simply want to change the logic to launch the process if it hasn't been launched for at least 7 days所以现在我只想更改逻辑以启动流程,如果它至少 7 天没有启动

That's what TaskScheduler is built to take care of.这就是 TaskScheduler 的目的。 No need to mess with date calculations or even language dependend formats of %date% .无需弄乱日期计算甚至%date%的语言依赖格式。

Start TaskScheduler启动任务调度器
"Create Task" (do not "Create Basic Task") “创建任务”(不要“创建基本任务”)
in "Triggers" set to "Weekly", "Recure every 1 weeks on: Monday在“触发器”中设置为“每周”,“每1周重复一次: Monday
in "Settings" check "Run task as soon as possible after a scheduled start is missed"在“设置”中选中“错过预定开始后尽快运行任务”
Fill also the "General", "Actions" and "Conditions" tabs to your needs.还根据您的需要填写“常规”、“操作”和“条件”选项卡。

(I have previously posted this in the comments because the question was locked. Since it is now unlocked I am submitting this as an answer) (我之前已经在评论中发布了这个,因为问题被锁定了。由于它现在已解锁,我将其提交为答案)

Batch scripting language does not have any built-in means to calculate time difference.批处理脚本语言没有任何内置的计算时间差的方法。 This problem may be solved through various means such as:这个问题可以通过多种方式解决,例如:

  • Write it using other scripting languages such as VBS, AutoIt, PowerShell, etc.使用其他脚本语言编写它,例如 VBS、AutoIt、PowerShell 等。
  • Download existing external program such as unix date utility from gnuwin32 coreutils ( manual , recipe ).gnuwin32 coreutils ( manual , recipe ) 下载现有的外部程序,例如 unix date实用程序。 Use for to get output of date "+%%s" into a variable, then use set /a to calculate the difference.使用fordate "+%%s"的输出转换为变量,然后使用set /a计算差异。
  • Parse output from built-in date command or %date% variable.解析内置date命令或%date%变量的输出。 This is discussed in great detail elsewhere ( A , B , C , D , E ) but be aware that it may be affected by OS locale time/date representation, and it is also hard to take leap years into account properly if you need precision.这在其他地方( ABCDE )进行了详细讨论,但请注意,它可能会受到操作系统语言环境时间/日期表示的影响,如果您需要精确度,也很难正确考虑闰年.

I'd like to share another trick: using file creation date difference.我想分享另一个技巧:使用文件创建日期差异。 It is still a somewhat of a hack, but is arguably more portable than parsing %date% by hand.它仍然有点像 hack,但可以说比手动解析%date%更便携。

Here is an example, explanation below:这是一个例子,解释如下:

if not exist "c:\some\marker.txt" goto RunTheJob
robocopy /minage:7 "c:\some\marker.txt" "c:\some\test.txt"
if exist "c:\some\test.txt" goto RunTheJob
goto SkipTheJob

:RunTheJob
del /y "c:\some\test.txt"
echo 1 >"c:\some\marker.txt"
rem ... THIS RUNS ONCE IN 7 DAYS ...

:SkipTheJob

Whenever we run the job (ie once in 7 days) we also create (or update) a "marker" file ( c:\some\marker.txt in the example).每当我们运行作业(即每 7 天运行一次)时,我们还会创建(或更新)一个“标记”文件(示例中为c:\some\marker.txt )。 Contents and location of this file do not matter, only date of its modification does, so I just write character 1 into it with echo 1 >YOUR_FILENAME这个文件的内容和位置无关紧要,只有修改日期才重要,所以我只是用echo 1 >YOUR_FILENAME将字符1写入其中

This also neatly solves the problem of storing date information somewhere.这也巧妙地解决了在某处存储日期信息的问题。

Now to test if at least 7 days passed since the job last ran you need to check if the file is at least 7 days old.现在要测试自上次运行作业以来是否至少过去了 7 天,您需要检查文件是否至少存在 7 天。 Easiest way is with robocopy utility - its /minage:NNN parameter only copies the file if it is at least NNN days old.最简单的方法是使用robocopy实用程序 - 它的/minage:NNN参数仅在文件至少为 NNN 天时复制文件。

So we periodically try to copy the file and see if it succeeds.所以我们会定期尝试复制文件,看看是否成功。 We do this by checking if copied file ( c:\some\test.txt ) exists like this: if exist YOUR_FILENAME goto YOUR_LABEL我们通过检查复制的文件( c:\some\test.txt )是否存在来做到这一点: if exist YOUR_FILENAME goto YOUR_LABEL

Finally, if the file was copied it is no longer needed and we need to delete it with del /y YOUR_FILENAME .最后,如果文件已被复制,则不再需要它,我们需要使用del /y YOUR_FILENAME将其删除。 /y overrides confirmations just in case and may normally be omitted. /y覆盖确认以防万一,通常可以省略。

If the marker file is ever deleted for any reason, the first line in the example makes sure that job (I'm assuming some kind of backup or something like that) is started right away.如果标记文件因任何原因被删除,则示例中的第一行确保该作业(我假设某种备份或类似的东西)立即启动。 If it wasn't there, the file would never be copied because the original would not exist in the first place, so the job would never run in this situation.如果它不存在,该文件将永远不会被复制,因为原始文件本来就不存在,因此该作业将永远不会在这种情况下运行。

Quirks:怪癖:

If some program updates the modification date of a "marker" file for some reason, the job will be delayed.如果某些程序出于某种原因更新了“标记”文件的修改日期,则该作业将被延迟。 If this is undesired you should put this file somewhere out of reach, such as %AppData%\Roaming\YOUR_SCRIPT_NAME folder.如果不希望这样做,您应该将此文件放在遥不可及的地方,例如%AppData%\Roaming\YOUR_SCRIPT_NAME文件夹。 Same thing may happen if the file gets restored from a backup.如果文件从备份中恢复,同样的事情也可能发生。

To work around such a problem you may consider compressing the marker file into an archive such as zip , but this also requires external programs, VBS or PowerShell.要解决此类问题,您可以考虑将标记文件压缩到诸如zip之类的存档中,但这也需要外部程序、VBS 或 PowerShell。 It might be possible to somehow abuse built-in Windows Backup utility ( sdclt ) instead but I have not found a way.可能会以某种方式滥用内置的 Windows 备份实用程序 ( sdclt ),但我还没有找到方法。

I have the same problem as Asif ALi, but neither Task Scheduler nor robocopy worked for me.我和 Asif ALi 有同样的问题,但 Task Scheduler 和 robocopy 都没有为我工作。

Task Scheduler cannot shedula a task for running at logon AND once in 7 days.任务计划程序无法安排在登录时运行的任务以及每 7 天运行一次。 Either is possible, but both don't.两者都有可能,但两者都不可能。

Robocopy interprets my file names as folders, therefore doesn't find them. Robocopy 将我的文件名解释为文件夹,因此找不到它们。 I used the same syntax as Jack suggested.我使用了与 Jack 建议的相同的语法。

robocopy /minage:7 "D:\Backups\Browsers\lastbackup.txt" "D:\Backups\Browsers\datest.txt"

result :结果 :

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Mon Jun 13 02:09:01 2022

   Source : D:\Backups\Browsers\lastbackup.txt\
     Dest : D:\Backups\Browsers\datest.txt\

    Files : *.*

  Options : *.* /COPY:DAT /MINAGE:7 /R:1000000 /W:30

------------------------------------------------------------------------------

2022/06/13 02:09:01 ERROR 123 (0x0000007B) Accessing Source Directory D:\Backups
\Browsers\lastbackup.txt\
The filename, directory name, or volume label syntax is incorrect.
Could Not Find d:\Backups\Browsers\datest.txt

I have Windows 7 Ultimate x64.我有 Windows 7 Ultimate x64。 Printed out robocopy /?打印出 robocopy /? but didn't find the right syntax.但没有找到正确的语法。


UPDATE 2022.06.14.更新 2022.06.14。 16:41 : 16:41:

I successfully managed to make use of robocopy this way :我以这种方式成功地使用了 robocopy:

set testfolder=datest_%date%
rem timeout 10
cd D:\Backups\Browsers\
if not exist lastbackup.txt goto dobackup
robocopy /minage:7 "D:\Backups\Browsers" "%testfolder%" lastbackup.txt
if not exist %testfolder%\lastbackup.txt goto end

:dobackup
echo.
echo Starting backup process
...
echo %date% >> lastbackup.txt

:end
rd /s /q %testfolder%
rem echo That's all folks ...

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

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