?
Solved

Calculating the Day of The Year?

Posted on 2013-06-06
11
Medium Priority
?
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
11 Comments
 
LVL 56

Accepted Solution

by:
Bill Prew earned 1336 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 664 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 56

Assisted Solution

by:Bill Prew
Bill Prew earned 1336 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

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

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 56

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…

770 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