Getting yesterday DATE


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting yesterday DATE
# 22  
Old 12-11-2009
I am using the below script in a production machine in a backup script and it works pretty well, on 1st of Jan and 1st of March (leap years) it works correctly, considers leap year well, try it

Code:
#! /usr/bin/ksh

OFFSET=${1:-1}

case $OFFSET in
  *[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year
day=$((day - OFFSET))
if (( day <= 0 )) ;then
  month=$((month - 1))
  if (( month == 0 )) ;then
    year=$((year - 1))
    month=12
  fi
  set -A days `cal $month $year`
  xday=${days[$(( ${#days[*]}-1 ))]}
  day=$((xday + day))
fi

print $year$month$day

This User Gave Thanks to EAGL€ For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

2. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

3. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

4. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

5. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

6. Shell Programming and Scripting

yesterday's date

curdate=$(date +"%d-%b-%y") How to get the yesterday's date. (1 Reply)
Discussion started by: sandy1028
1 Replies

7. Shell Programming and Scripting

yesterday's date

I was playing to find a simple way to get yesterday's date, and came up with this (on an AIX 5.2 box): $ date Thu Feb 19 11:21:26 EST 2009 $ echo $TZ EST5EDT $ yesterday=`TZ=$(date +%Z)+24 date` $ echo $yesterday Wed Feb 18 16:21:52 GMT 2009 Why it is converted to GMT instead of... (2 Replies)
Discussion started by: gratus
2 Replies

8. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

9. Shell Programming and Scripting

yesterday date month/date

Hi expert, I want to retrieve yesterday su log. How to calculate and assign variable value ( 06/23 ) in myVariable ? #!/bin/sh myVariable=yesterday date in month/date cat /var/adm/sulog | grep $myVariable > file.txt many thanks! (5 Replies)
Discussion started by: skully
5 Replies

10. Shell Programming and Scripting

get yesterday's date?

Hello, using date, we can easily get today's date $ date +%y-%m-%d 06-12-08 is it possible for me to get yesterday's date using 'date', if not, is there any quick and easy way to do that? Thanks! (1 Reply)
Discussion started by: fedora
1 Replies
Login or Register to Ask a Question
Date::Simple(3pm)					User Contributed Perl Documentation					 Date::Simple(3pm)

NAME
Date::Simple - a simple date object SYNOPSIS
use Date::Simple ('date', 'today'); # Difference in days between two dates: $diff = date('2001-08-27') - date('1977-10-05'); # Offset $n days from now: $date = today() + $n; print "$date "; # uses ISO 8601 format (YYYY-MM-DD) use Date::Simple (); my $date = Date::Simple->new('1972-01-17'); my $year = $date->year; my $month = $date->month; my $day = $date->day; use Date::Simple (':all'); my $date2 = ymd($year, $month, $day); my $date3 = d8('19871218'); my $today = today(); my $tomorrow = $today + 1; if ($tomorrow->year != $today->year) { print "Today is New Year's Eve! "; } if ($today > $tomorrow) { die "warp in space-time continuum"; } print "Today is "; print(('Sun','Mon','Tues','Wednes','Thurs','Fri','Satur') [$today->day_of_week]); print "day. "; # you can also do this: ($date cmp "2001-07-01") # and this ($date <=> [2001, 7, 1]) DESCRIPTION
Dates are complex enough without times and timezones. This module may be used to create simple date objects. It handles: Validation. Reject 1999-02-29 but accept 2000-02-29. Interval arithmetic. How many days were between two given dates? What date comes N days after today? Day-of-week calculation. What day of the week is a given date? Transparent date formatting. How should a date object be formatted. It does not deal with hours, minutes, seconds, and time zones. A date is uniquely identified by year, month, and day integers within valid ranges. This module will not allow the creation of objects for invalid dates. Attempting to create an invalid date will return undef. Month numbering starts at 1 for January, unlike in C and Java. Years are 4-digit. Gregorian dates up to year 9999 are handled correctly, but we rely on Perl's builtin "localtime" function when the current date is requested. On some platforms, "localtime" may be vulnerable to rollovers such as the Unix "time_t" wraparound of 18 January 2038. Overloading is used so you can compare or subtract two dates using standard numeric operators such as "==", and the sum of a date object and an integer is another date object. Date::Simple objects are immutable. After assigning $date1 to $date2, no change to $date1 can affect $date2. This means, for example, that there is nothing like a "set_year" operation, and "$date++" assigns a new object to $date. This module contains various undocumented functions. They may not be available on all platforms and are likely to change or disappear in future releases. Please let the author know if you think any of them should be public. Controlling output format. As of version 3.0 new ways of controlling the output formats of Date::Simple objects has been provided. However Date::Simple has traditionally provided few ways of stringification, a primary one via the format() method and another primary one via direct stringification. However the later is currently implemented as an XS routine and the former is implemented through a perl routine. This means that using format() is more expensive than stringification and that the stringification format is class specific. In order to alleviate some of these problems a new mechanism has been introduced to Date::Simple that allows for a per object level format default. In addition a set of utility classes that have different stringification overloads provided. These classes are simple subclasses of Date::Simple and beside the default format() and the overloaded stringification behaviour are identical to Date::Simple. In fact one is totally identical to Date::Simple and is provided mostly for completeness. The classes included are: Date::Simple::ISO Identical to Date::Simple in every respect but name. Date::Simple::D8 Uses the D8 format (%Y%m%d) as the default format for printing. Uses XS for the overloaded stringification. Date::Simple::Fmt Uses the perl implemented format() as the default stringification mechanism. The first argument to the constructor is expected to be the format to use for the object. NOTE its important to remember that the primary difference between the behaviour of objects of the different classes is how they are stringified when quoted, and what date format is used by default when the format() method is called. Nothing else differs. CONSTRUCTORS
Several functions take a string or numeric representation and generate a corresponding date object. The most general is "new", whose argument list may be empty (returning the current date), a string in format YYYY-MM-DD or YYYYMMDD, a list or arrayref of year, month, and day number, or an existing date object. Date::Simple->new ([ARG, ...]) date ([ARG, ...]) my $date = Date::Simple->new('1972-01-17'); The "new" method will return a date object if the values passed in specify a valid date. (See above.) If an invalid date is passed, the method returns undef. If the argument is invalid in form as opposed to numeric range, "new" dies. The "date" function provides the same functionality but must be imported or qualified as "Date::Simple::date". (To import all public functions, do "use Date::Simple (':all');".) This function returns undef on all invalid input, rather than dying in some cases like "new". date_fmt (FMT,[ARG, ...]) Equivelent to "date" but creates a Date::Simple::Fmt object instead. The format is expected to be a valid POSIX::strftime format string. date_iso ([ARG, ...]) Identical to "date" but creates a Date::Simple::ISO object instead. date_d8 ([ARG, ...]) Equivelent to "date" but creates a Date::Simple::D8 object instead. today() Returns the current date according to "localtime". Caution: To get tomorrow's date (or any fixed offset from today), do not use "today + 1". Perl parses this as "today(+1)". You need to put empty parentheses after the function: "today() + 1". ymd (YEAR, MONTH, DAY) Returns a date object with the given year, month, and day numbers. If the arguments do not specify a valid date, undef is returned. Example: use Date::Simple ('ymd'); $pbd = ymd(1987, 12, 18); d8 (STRING) Parses STRING as "YYYYMMDD" and returns the corresponding date object, or undef if STRING has the wrong format or specifies an invalid date. Example: use Date::Simple ('d8'); $doi = d8('17760704'); Mnemonic: The string matches "/d{8}/". Also, "d8" spells "date", if 8 is expanded phonetically. INSTANCE METHODS
DATE->next my $tomorrow = $today->next; Returns an object representing tomorrow. DATE->prev my $yesterday = $today->prev; Returns an object representing yesterday. DATE->year my $year = $date->year; Return the year of DATE as an integer. DATE->month my $month = $date->month; Return the month of DATE as an integer from 1 to 12. DATE->day my $day = $date->day; Return the DATE's day of the month as an integer from 1 to 31. DATE->day_of_week Return a number representing DATE's day of the week from 0 to 6, where 0 means Sunday. DATE->as_ymd my ($year, $month, $day) = $date->as_ymd; Returns a list of three numbers: year, month, and day. DATE->as_d8 Returns the "d8" representation (see "d8"), like "$date->format("%Y%m%d")". DATE->as_iso Returns the ISO 8601 representation of the date (eg '2004-01-01'), like "$date->format("%Y-%m-%d")". This is in fact the default overloaded stringification mechanism and is provided mostly so other subclasses with different overloading can still do fast ISO style date output. DATE->as_str ([STRING]) DATE->format ([STRING]) DATE->strftime ([STRING]) These functions are equivalent. Return a string representing the date, in the format specified. If you don't pass a parameter, the default date format for the object is used if one has been specified, otherwise uses the default date format for the class the object is a member of, or as a last fallback uses the $Date::Simple::Standard_Format which is changeable, but probably shouldn't be modified. See "default_format" for details. my $change_date = $date->format("%d %b %y"); my $iso_date1 = $date->format("%Y-%m-%d"); my $iso_date2 = $date->format; The formatting parameter is similar to one you would pass to strftime(3). This is because we actually do pass it to strftime to format the date. This may result in differing behavior across platforms and locales and may not even work everywhere. DATE->default_format ([FORMAT]) This method sets or gets the default_format for the DATE object or class that it is called on. OPERATORS
Some operators can be used with Date::Simple instances. If one side of an expression is a date object, and the operator expects two date objects, the other side is interpreted as "date(ARG)", so an array reference or ISO 8601 string will work. DATE + NUMBER DATE - NUMBER You can construct a new date offset by a number of days using the "+" and "-" operators. DATE1 - DATE2 You can subtract two dates to find the number of days between them. DATE1 == DATE2 DATE1 < DATE2 DATE1 <=> DATE2 DATE1 cmp DATE2 etc. You can compare two dates using the arithmetic or string comparison operators. Equality tests ("==" and "eq") return false when one of the expressions can not be converted to a date. Other comparison tests die in such cases. This is intentional, because in a sense, all non-dates are not "equal" to all dates, but in no sense are they "greater" or "less" than dates. DATE += NUMBER DATE -= NUMBER You can increment or decrement a date by a number of days using the += and -= operators. This actually generates a new date object and is equivalent to "$date = $date + $number". "$date" You can interpolate a date instance directly into a string, in the format specified by ISO 8601 (eg: 2000-01-17) for Date::Simple and Date::Simple::ISO, for Date::Simple::D8 this is the same as calling as_d8() on the object, and for Date::Simple::Fmt this is the same as calling format() on the object. UTILITIES
leap_year (YEAR) Returns true if YEAR is a leap year. days_in_month (YEAR, MONTH) Returns the number of days in MONTH, YEAR. leap_year (YEAR) Returns true if YEAR is a leap year. days_in_month (YEAR, MONTH) Returns the number of days in MONTH, YEAR. AUTHOR
Marty Pauley <marty@kasei.com> John Tobey <jtobey@john-edwin-tobey.org> Yves Orton <demerphq@hotmail.com> COPYRIGHT
Copyright (C) 2001 Kasei. Copyright (C) 2001,2002 John Tobey. Copyright (C) 2004 Yves Orton. This program is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License; either version 2 of the License, or (at your option) any later version. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA b) the Perl Artistic License. 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 ALSO
Date::Simple::Fmt Date::Simple::ISO Date::Simple::D8 and of course perl perl v5.14.2 2008-01-11 Date::Simple(3pm)