Hereok
Hereok

Reputation: 41

batch script that converts date to julian date

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

Upvotes: 0

Views: 5595

Answers (3)

Kris Ellis Lewis
Kris Ellis Lewis

Reputation: 21

@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

Upvotes: 1

Aacini
Aacini

Reputation: 67216

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%

Upvotes: 2

npocmaka
npocmaka

Reputation: 57262

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"

Upvotes: 1

Related Questions