Improve company productivity with a Business Account.Sign Up

x
?
Solved

Calculating the Day of The Year?

Posted on 2013-06-06
11
Medium Priority
?
453 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
10 Comments
 
LVL 61

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 61

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

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

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 61

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
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). …

595 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