Solved

Calculating the Day of The Year?

Posted on 2013-06-06
11
433 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 53

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 53

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 1

Author Comment

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

Expert Comment

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

~bp
0
 
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 53

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

825 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