Packetwatch.net

Display previous date used for scripting



Last modified: Nov. 18, 2011

Contents
1 - Summary
2 - FreeBSD
3 - Linux
4 - OpenBSD
5 - Solaris
6 - Windows


1 - Summary

This guide will show you how to display the previous date which is useful when
writing scripts. This has been tested in FreeBSD, Linux, Solaris and Windows
XP, 2003 and 2008.

2 - FreeBSD

Here is a script that displays the previous date.

  #!/bin/sh

  date=/bin/date
  echo=/bin/echo

  date_set=`$date -v -1d +%m/%d/%y`
  $echo Previous date: $date_set
  exit 0


3 - Linux

Here is a script that displays the previous date.

  #!/bin/sh

  date=/bin/date
  echo=/bin/echo

  date_set=`$date --date='-1 day' +%m/%d/%y`
  $echo Previous date: $date_set
  exit 0


4 - OpenBSD

Here is a script that displays the previous date.

  #!/bin/sh

  date=/bin/date
  echo=/bin/echo

  date_set=`TZ="GMT+24" $date +'%m/%d/%y'`
  $echo Previous date: $date_set
  exit 0


5 - Solaris

Here is a script that displays the previous date.

  #!/bin/sh

  date=/usr/bin/date
  echo=/usr/bin/echo

  date_set=`TZ="GMT+24" $date +'%m/%d/%y'`
  $echo Previous date: $date_set
  exit 0


6 - Windows

Here is a script that displays the previous date. A few things are needed for
this to work. You will need the date and cut utilities from the GnuWin32
website located at http://gnuwin32.sourceforge.net/. These utilities are part
of CoreUtils. The dependencies zip file is also needed.

  @echo off

  set date="C:\Program Files\System Utilities\bin\date.exe"
  set cut="C:\Program Files\System Utilities\bin\cut.exe"

  %date% --date="-1 day" --iso-8601 > %previous_date_results%#
  for /f "tokens=1" %%x in (%previous_date_results%#) do (set previous_date=%%x)
  del /f /q # > nul 2>&1
  echo %previous_date% | %cut% -d - -f 2 > %month_results%#
  for /f "tokens=1" %%x in (%month_results%#) do (set month=%%x)
  del /f /q # > nul 2>&1
  echo %previous_date% | %cut% -d - -f 3 > %day_results%#
  for /f "tokens=1" %%x in (%day_results%#) do (set day=%%x)
  del /f /q # > nul 2>&1
  echo %previous_date% | %cut% -d - -f 1 > %year_results%#
  for /f "tokens=1" %%x in (%year_results%#) do (set year=%%x)
  del /f /q # > nul 2>&1
  set date_set=%month%/%day%/%year%
  echo Previous date: %date_set%
  pause
  exit


Last modified: Thu Jan 1 00:00:00 1970 UTC
Packetwatch Research 2002-2024.