Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Calculating the Day of The Year?

Hi All,

Here is what I am trying to do, I would like to set a variable of the following:

VariableName IRSYYDDD

Where YY = Current year (two digits) = 13
Where DDD = Current day of the year (three digits - calculated based on the day of the year, today is day 157).

I have no idea how I would do this ...the variable will be used in a larger batch script. I can execute other programs through the cmd line if necessary.

I would greatly appreciate any assistance on this.

Thanks,
Ryan
0
Ryan Rood
Asked:
Ryan Rood
  • 4
  • 4
  • 2
3 Solutions
 
Bill PrewCommented:
Here you go.

@echo off

set YY=%DATE:~-2%
call :dayofYear DDD
set myVar=IRS%YY%%DDD%
echo %myVar%

exit /b


:dayOfYear JD DateStr -- returns the day of the year, i.e. 1 for 1/1/2008, 266 for 12/31/2008
::                    -- day     [out,opt] - variable name to store resulting day of the year
::                    -- DateStr [in,opt]  - date string, e.g. "3/31/2006" or "Fri 03/31/2006" or "31.3.2006", or omit form current date
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20080612 :$changed 20080612 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "DateStr=%~2"&if "%~2"=="" set "DateStr=%date%"
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
set /a "yy=10000%yy% %%10000,mm=1,dd=1"
set /a JD-=-1+dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

Open in new window

~bp
0
 
Steve KnightIT ConsultancyCommented:
Easiest way is to use a bit of VBScript to take 1/1/2013 from current date using techniques like from here:  http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html


@echo off
echo daynum=datediff("d",dateserial(year(date),1,1),date) > "%temp%\dateparts.vbs"
echo wscript.echo "IRS" ^& right(datepart("yyyy",date),2) ^& right(1000+daynum,3) ^& "," ^& daynum >> "%temp%\dateparts.vbs"
for /f "tokens=1,2 delims=," %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do (set today=%%~a) & (set daynum=%%~b)

echo Todays filename is %today% which is day number %daynum%

Open in new window

0
 
Bill PrewCommented:
With all due respect to dragon-it (Steve knows I love him!), if you wanted to leverage the stronger date capabilities of VBscript from BAT to do this, here's a technique I like.  It creates a very small VBS file in the %TEMP% folder that merely evaluates whatever VBS statement is passed to it and returns the result. Then I have a small subroutine in the BAT file that can be used many times to send VBS code to the script, and places the output in the %eval% variable.

Anyplace you would normally use double quotes in the VBS command string just use single quotes.

@echo off

set YY=%DATE:~-2%
call :Eval "'IRS' & Right(Datepart('yyyy',Date),2) & Right('00'&Datepart('y',Date),3)"
set myVar=%eval%
echo %myVar%

exit /b

:Eval [expression]
  if not exist "%TEMP%\_eval.vbs" echo Wscript.Echo Eval(Replace(WScript.Arguments.Item(0),Chr(39),Chr(34)))>"%TEMP%\_eval.vbs"
  for /F "tokens=*" %%A in ('cscript //nologo %TEMP%\_eval.vbs "%~1"') do set eval=%%A
  exit /b

Open in new window

~bp
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Ryan RoodAuthor Commented:
Fantastic! Works exactly as expected. Thank you very much.
0
 
Bill PrewCommented:
Great, glad that helped, thanks for the feedback.

~bp
0
 
Steve KnightIT ConsultancyCommented:
yes i use such techniques too Bill.

Bill - I still dont like doing complex maths ways of doing string operations on dates when there are perfectly good real date handling built into the computer.

obviously i posted mine at the same time as Bill before but did you even try it?  If you do work with dates in your scripting do consider using VBScript as a whole or for relevant parts - if you'd been through the pain (and many still are) of companies with people in UK, US, Japanese date formats across multiple OS with and without leding zeroes on dates etc. it gets tedious tracking down date code written using string handling in lots of different application languages.

had some recently where the VB like code did things like:

date= (some string handling) then used date as a variable in the code.

Now date= sets the system date... and caused odd issues on xp clients that had this right and the applicaton to crash on Windows U where they didn't.  The same app. was found to not work during oct/nov/dec and before 10th of month etc. due to dates beig interpreted as dd/mm or mm/dd ...

clearly digressed, while I sit here waiting on phone, sorry!

steve
0
 
Ryan RoodAuthor Commented:
Hi Steve,

I have evaluated your answer as well. I did not the first time I reviewed. Both are working. I have requested a review and accept both as answers.

Thanks again to both of you.
0
 
Bill PrewCommented:
@coppsbuildall,

Props to you for taking extra time to re-evaluate the way you closed the question, glad to see you recognizing the efforts of other experts, well done.

bp
0
 
Steve KnightIT ConsultancyCommented:
Thanks too, I wasn't expecting this to be re-opened.
0
 
Steve KnightIT ConsultancyCommented:
Thanks, appreciate it.

Steve
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now