Skip to content

Instantly share code, notes, and snippets.

@FlorianWolters
Created April 27, 2012 14:21
Show Gist options
  • Save FlorianWolters/2509663 to your computer and use it in GitHub Desktop.
Save FlorianWolters/2509663 to your computer and use it in GitHub Desktop.
Prints out the last date of the last month in optional DIN 5008 format.
@ECHO OFF
REM Prints out the last date of the last month in optional DIN 5008 format.
REM
REM The optional DIN 5008 format is "dd.mm.yyyy", e.g. "29.02.2012".
REM
REM Author: Florian Wolters <[email protected]>
REM Link..: http://github.com/FlorianWolters
REM
REM Copyright (C) 2012 by Florian Wolters. All rights reserved.
REM
REM This program is free software: you can redistribute it and/or modify it
REM under the terms of the GNU Lesser General Public License as published by the
REM Free Software Foundation, either version 3 of the License, or (at your
REM option) any later version.
REM
REM This program is distributed in the hope that it will be useful, but WITHOUT
REM ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
REM FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
REM for more details.
REM
REM You should have received a copy of the GNU Lesser General Public License
REM along with this program. If not, see http://gnu.org/licenses/lgpl.txt.
SETLOCAL
REM Uncomment and set for tests.
REM SET DATE=01.03.2012
REM Set local variables.´
SET CURRENT_DAY=%DATE:~0,2%
SET CURRENT_MONTH=%DATE:~3,2%
SET CURRENT_YEAR=%DATE:~6,4%
SET RESULT_DAY=31
SET /A RESULT_MONTH=%CURRENT_MONTH%-1
SET RESULT_YEAR=%CURRENT_YEAR%
REM If the current month is January, the last month is December of the previous year.
IF %CURRENT_MONTH% == 01 (
SET RESULT_MONTH=12
SET /A RESULT_YEAR=%CURRENT_YEAR%-1
GOTO EOF
)
IF %RESULT_MONTH% LSS 10 SET RESULT_MONTH=0%RESULT_MONTH%
FOR %%? IN (04 06 09 11) DO IF %RESULT_MONTH% == %%? SET RESULT_DAY=30
IF NOT %RESULT_MONTH% == 02 GOTO EOF
REM Check whether the year is a leap year.
:CHECK_LEAP_YEAR
SET /A ISLEAP=%RESULT_YEAR% %% 4
IF NOT %ISLEAP% == 0 GOTO NOT_LEAP_YEAR
SET /A ISLEAP=%RESULT_YEAR% %% 100
IF NOT %ISLEAP% == 0 GOTO IS_LEAP_YEAR
SET /A ISLEAP=%RESULT_YEAR% %% 400
IF NOT %ISLEAP% == 0 GOTO NOT_LEAP_YEAR
:IS_LEAP_YEAR
SET RESULT_DAY=29
GOTO EOF
:NOT_LEAP_YEAR
SET RESULT_DAY=28
:EOF
SET RESULT=%RESULT_DAY%.%RESULT_MONTH%.%RESULT_YEAR%
ECHO %RESULT%
@FlorianWolters
Copy link
Author

Requirements

  • Microsoft Windows version 6.1.7601 ("Microsoft Windows 7")

NOTICE: May also work with older versions.

Usage

  1. Write the result to the standard output (STDOUT).
lastDateOfLastMonth.cmd
  1. Write the result to a text file.
lastDateOfLastMonth.cmd > debug.log

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://gnu.org/licenses/lgpl.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment