0

Im creating a batch file to convert %date% into julian date but I seem to not find clear information on to do this. Anyone can tell me how to convert %date% to julian date? or any batch file that exist that already those this?

Thank you

Hereok
  • 41
  • 4
  • 12
  • I am pretty sure there is a batch file out on the web to convert a date into just about any format you want. – Squashman Jan 13 '16 at 20:58
  • If you hover over the julian-date tag it says: The term "Julian date" also refers to the day-of-year number (more properly, the **ordinal date**) in the Gregorian calendar, especially in computer programming, the military and the food industry. – Squashman Jan 13 '16 at 22:44

3 Answers3

2

EDIT: Original code modified to just show the Julian Day Number of current date in "Dow MM/DD/YYYY" format:

@echo off
for /F "tokens=2-4 delims=/ " %%a in ("%date%") do (
   set /A "a=(%%a-14)/12, JDN=(1461*(%%c+4800+a))/4+(367*(%%a-2-12*a))/12-(3*((%%c+4900+a)/100))/4+%%b-32075"
)
echo %JDN%

Reference: http://www.hermetic.ch/cal_stud/jdn.htm#comp

2nd. EDIT

To get the ordinal day number, based on the number of days in the year (from 1 to 366), use this method:

@echo off
setlocal

for /F "tokens=2-4 delims=/ " %%a in ("%date%") do (
   set /A "MM=1%%a-100, DD=1%%b-100, Ymod4=%%c%%4"
)
for /F "tokens=%MM%" %%m in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A Day=DD+%%m
if %Ymod4% equ 0 if %MM% gtr 2 set /A Day+=1

echo %Day%
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • It returns a Missing operand error when I run that batch. – Hereok Jan 13 '16 at 21:25
  • Of course! This is _not_ a working Batch file, but just two subroutines that do the conversion process. If you want a _complete_ Batch file, then you first show us what is the format of the `%date%` you want to convert: MM/DD/YYYY, DD/MM/YYYY, any other? Is the date the current one? Is it given as parameter? Etc... – Aacini Jan 13 '16 at 21:29
  • Example of the %date% format from the command prompt: >echo %date% >Wed 01/13/2016 – Hereok Jan 13 '16 at 22:00
  • It returns 2457401 I think its incorrect, I believe its suppose to return 013 which is today julian date. – Hereok Jan 13 '16 at 22:14
  • @Hereok You are confusing Julian date with Ordinal date. – Squashman Jan 13 '16 at 22:17
  • Im referen to this julian date [Please view this link](http://computerequipment.tpub.com/TM-11-5895-1412-12P/img/TM-11-5895-1412-12P_243_1.jpg) – Hereok Jan 13 '16 at 22:22
  • Nope. That is ordinal date. Read Wikipedia. https://en.wikipedia.org/wiki/Julian_day and https://en.wikipedia.org/wiki/Ordinal_date – Squashman Jan 13 '16 at 22:27
  • Well, if this answer give the same result of the other answer (2457401), both methods should be correct, right? Did you read the description at the `julian` tag you used in your question? "Now, April 13, 2011 (UTC) the Julian day number is 2455665". Anyway, read my second edit... – Aacini Jan 13 '16 at 22:40
  • @Aacini, that is much shorter code then the one I had been using from this site: http://www.commandline.co.uk/cmdfuncs/dandt/#datetoordinal – Squashman Jan 13 '16 at 22:42
1

With this here's a julian.bat:

@if (@X)==(@Y) @end /* JScript comment
    @echo off

    cscript //E:JScript //nologo "%~f0" 

    exit /b %errorlevel%

@if (@X)==(@Y) @end JScript comment */


Date.prototype.getJulian = function() {
    return Math.floor((this / 86400000) - (this.getTimezoneOffset()/1440) + 2440587.5);
}

var today = new Date(); //set any date
var julian = today.getJulian(); //get Julian counterpart
WScript.Echo(julian);

And it can be used like:

for /f %%a in ('call julian.bat') do @set "julian=%%a"
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Hello I tried that script and the result is 2457401 I believe that incorrect. It shoul return 013 which is today julian date. Any thought in why its returning 2457401? – Hereok Jan 13 '16 at 21:15
  • @Hereok I've tried [this](http://www.onlineconversion.com/julian_date.htm) and [this](http://aa.usno.navy.mil/data/docs/JulianDate.php) and few more and results are consistent with these in the script – npocmaka Jan 14 '16 at 08:50
1

@Aacini - your answer works perfectly ... unless month or date is 08 or 09 - then batch gets confused and thinks it's an attempt at an illegal octal. I do not see how to keep it on a single line anymore, but here's a solution (tested, and incorporating @jeb comment):

:get_julian
    for /F "tokens=1-3 delims=/" %%a in ("%1") do (
        set /a mon=100%%a %% 100
        set /a day=100%%b %% 100
        set year=%%c
    )
    set /a x=(mon-14)/12
    set /a JD=(1461*(year+4800+x))/4+(367*(mon-2-12*x))/12-(3*((year+4900+x)/100))/4+day-32075
    exit /b