"universal" %DATE% parser

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: "universal" %DATE% parser

#16 Post by aGerman » 16 May 2011 17:03

Well Dave, don't get me wrong -- this is a batch forum and I appreciate pure batch solutions. My objection was if you use JScript you should make as simple as you can. Normally I try to avoid those chimeras because they slow down your batch file enormously. If there is no (good) possibility with pure batch I prefer writing the whole thing in VBScript or C++.
How ever.
You could use functions in the JScript and then call one of them depending on an argument.

Code: Select all

@set @junk=0 /* The 1st 3 lines should not be changed
@echo off & set "@junk="
setlocal

cscript //nologo //e:jscript "%~f0" year
cscript //nologo //e:jscript "%~f0" month
cscript //nologo //e:jscript "%~f0" day

pause
goto :eof
*/

var d = new Date();

var arg = WScript.Arguments(0)
if (arg == "year") year();
else if (arg == "month") month();
else if (arg == "day") day();
else WScript.Quit(1);

function year() {
  WScript.Echo(d.getFullYear());
}

function month() {
  WScript.Echo(d.getMonth() + 1);
}

function day() {
  WScript.Echo( d.getDate());
}

Regards
aGerman

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: "universal" %DATE% parser

#17 Post by !k » 17 May 2011 11:57

aGerman wrote:Sorry guys, but now I'm totally confused. On the right hand you use JScript-injection, on the left hand nobody has the idea to use it for getting the date directly

Image

:?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: "universal" %DATE% parser

#18 Post by aGerman » 17 May 2011 12:53

OMG, you're right !k :oops: I forgot getMonth() returns 0 for january and 11 for december, so we have to add 1. I will edit the codes above...

Thanks
aGerman

Post Reply