Windows console

前端 未结 6 427
刺人心
刺人心 2020-12-01 21:53

I would like to set the date in a Windows batch file to 7 days ago from today. I would like to do this in the following format.



        
6条回答
  •  盖世英雄少女心
    2020-12-01 22:43

    There's an answer with calling powershell. Though there are still machines running without powershell installed (at the moment of writing this XP,Vista,Windows7,Windows2003 and Windows2008 still have a descent OS share and are coming without installed powershell)

    Other option will be using jscript within a bat script

    Here's the dayAdder.bat that accepts only one argument - the days you want to add to the current date and prints the result:

      @if (@X) == (@Y) @end /* JScript comment 
        @echo off   
        cscript //E:JScript //nologo "%~f0" %*
        exit /b %errorlevel%       
    @if (@X)==(@Y) @end JScript comment */
    
    var days=parseInt(WScript.Arguments.Item(0));
    
    
    Date.prototype.addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
    }
    
    var date = new Date();
    
    WScript.Echo(date.addDays(5));
    WScript.Echo("Year: " + date.getFullYear());
    WScript.Echo("Month: " + date.getMonth());
    WScript.Echo("DayOfTeWEek: " + date.getDay());
    

    usage and the output:

    E:\scripts>call dayAdder.bat 7
    Sun Nov 8 16:27:48 UTC+0200 2020
    Year: 2020
    Month: 10
    DayOfTeWEek: 2
    DayOfTheMonth: 3
    

    You can modify it in way that will be suitable for you.

提交回复
热议问题