Solved

Calculating the Day of The Year?

Posted on 2013-06-06
11
431 Views
Last Modified: 2013-06-08
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
Comment
Question by:Ryan Rood
  • 4
  • 4
  • 2
11 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 334 total points
ID: 39227586
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 166 total points
ID: 39227602
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
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 334 total points
ID: 39227652
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
 
LVL 1

Author Comment

by:Ryan Rood
ID: 39227735
Fantastic! Works exactly as expected. Thank you very much.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39227742
Great, glad that helped, thanks for the feedback.

~bp
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39228096
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
 
LVL 1

Author Comment

by:Ryan Rood
ID: 39228675
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
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39228715
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39228808
Thanks too, I wasn't expecting this to be re-opened.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39232249
Thanks, appreciate it.

Steve
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now