4.6.5. Grab date & time to NppExec variable

1. To grab current date to NppExec variable, you can create the following simple auxiliary script:

NPE_CONSOLE local v+
cmd /c date /t
set DATE = $(OUTPUTL)

and give it a name "date", for example.

Then you can use "NPP_EXEC date" or just "\date" to call this script which sets an internal environment variable $(DATE).

The same approach can be used for time: just replace "date /t" with "time /t".

2. To grab date or time in user-specified format, some additional actions are needed. First, go to Notepad++'s folder (where notepad++.exe is located) and create a file named "date.bat" with the following content:

@echo off
for /f "tokens=1-3 delims=/.- " %%a in ('DATE /T') do set CUR_DATE=%%c%%b%%a
echo %CUR_DATE%

Then create the following NppExec's script which will use this bat-file:

NPE_CONSOLE local v+
"$(NPP_DIRECTORY)\date.bat"
set DATE = $(OUTPUTL) // the bat-file's output

Now your date format is definitely determined by this line:

for /f "tokens=1-3 delims=/.- " %%a in ('DATE /T') do set CUR_DATE=%%c%%b%%a

and you can change it as you want, e.g.

for /f "tokens=1-3 delims=/.- " %%a in ('DATE /T') do set CUR_DATE=%%c-%%b-%%a

or

for /f "tokens=1-3 delims=/.- " %%a in ('DATE /T') do set CUR_DATE=%%c%%a%%b

or whatever you want.

3. Finally, you can use the following bat-file which returns both date and time as one string:

@echo off
for /f "tokens=1-3 delims=/.- " %%a in ('DATE /T') do set CUR_DATE=%%c%%b%%a
for /f "tokens=1-2 delims=: " %%a in ('TIME /T') do set CUR_TIME=%%a%%b
echo %CUR_DATE%_%CUR_TIME%

4. Here is another version of the previous bat-file, now using cmd's environment variables %DATE% and %TIME% which supports also milliseconds:

@echo off
for /f "tokens=1-3 delims=/.- " %%a in ("%DATE%") do set CUR_DATE=%%c%%b%%a
for /f "tokens=1-4 delims=:., " %%a in ("%TIME%") do set CUR_TIME=%%a%%b%%c%%d
echo %CUR_DATE%_%CUR_TIME%

See also: Using cmd.exe [4.4].