Search code examples
windowsdatetimebatch-filedatetime-conversionjulian-date

Method to convert a MM/DD/YYYY into YYYYDD (julian day)


Is there any way on how to convert mm/dd/yyyy (my computer date) into yyyydd (julian day) in batch process?


Solution

  • @ECHO OFF
    SETLOCAL
    FOR %%y IN (2000 2001 2012 2013 2014 2015 2016) DO (
     FOR %%d IN (
      01/01/%%y 02/01/%%y 02/28/%%y 02/29/%%y 03/01/%%y 04/01/%%y 05/01/%%y
      06/01/%%y 07/01/%%y 08/01/%%y 09/01/%%y 10/01/%%y 11/01/%%y 12/01/%%y 12/31/%%y
               ) DO CALL :julian %%d
     )
    
    GOTO :EOF
    
    :julian
    SET jdate=%1
    SET /a yyyy=%jdate:~-4%&SET /a mm=1%jdate:~0,2%&SET jdate=%jdate:~-4%0%jdate:~3,2%
    SET /a yyyy=yyyy %% 4
    IF %yyyy% equ 0 IF %mm% gtr 102 SET /a jdate +=1
    FOR %%a IN (02:31 03:28 04:31 05:30 06:31 07:30 08:31 09:31 10:30 11:31 12:30) DO (
     FOR /f "tokens=1,2delims=:" %%b IN ("%%a") DO IF "1%%b" leq "%mm%" SET /a jdate+=%%c
    )
    ECHO %1 becomes %jdate%
    GOTO :eof
    

    The first part of this routine is simply generating dates including significant dates for selected years.

    The :julian routine actually does the conversion, having been passed the mm/dd/yyyy date from the main routine.

    Works for me!