LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-29-2011, 12:24 AM   #1
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Rep: Reputation: 0
Yesterday's date in unix


#!/usr/bin/perl
$presentdate=`date +"%m-%d-%Y"`;
@datearray=split('-',$presentdate);
$mm=$datearray[0];
$dd=$datearray[1];
$yy=$datearray[2];
if ( $dd > 2 )
{
$dd-=1;
}
elsif ( $mm == ( 5 || 7 || 8 || 10 || 12 ))
{
$dd = 30;
$mm = $mm-1;
}
elsif ( $mm == 1 )
{
$dd=31;
$mm=12;
$yy=$yy-1;
}
elsif ( $mm == 3 )
{
$rem=$yy%4;
$mm=$mm-1;
$dd=29 if $rem==0;
$dd=28 if $rem!=0;
}
elsif ( $mm == ( 2 || 4 || 6 || 9 || 11 ))
{
$dd=31;
$mm=$mm-1;
}
$date = ($mm . "-$dd" ."-$yy");
printf ("%s \n", $date);
 
Old 03-29-2011, 01:32 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
How about:
Code:
date --date="-1 days" +"%Y-%m-%d"
Evo2.
 
Old 03-29-2011, 01:43 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
lol

jlinkels
 
1 members found this post helpful.
Old 03-29-2011, 03:12 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Quote:
Originally Posted by jlinkels View Post
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
Wow. Seeing that rings a bell, I must a have read somewhere that date will take natural language. Seems that things like "last week" work too.

Cheers,

Evo2.
 
Old 03-29-2011, 03:40 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually the OP did not ask anything. The perl solution works and I guess the script is meant to be used on Unix machines where the date command lacks the -d, --date option. I used a C program in the good ol' days of IRIX or SUN SPARC for the same reason.
 
Old 03-29-2011, 05:06 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Quote:
Originally Posted by colucix View Post
Actually the OP did not ask anything. The perl solution works
The OPs solution does not treat leap years correctly.
See for example: http://en.wikipedia.org/wiki/Leap_year#Algorithm

Cheers,

Evo2.
 
Old 03-30-2011, 12:57 AM   #7
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Original Poster
Rep: Reputation: 0
Thnx everyone for the response , I was looking for something that works universally on hpux ,solaris and linux ,the options provided above work on linux mainly and over a leap year I was not sure so I thought it would be better to use something universally valid .
 
Old 03-31-2011, 12:09 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by jlinkels View Post
The OP asked about yesterday's date:
Code:
date -d "yesterday"  +"%Y-%m-%d"
lol

jlinkels
If you want some real fun:
Code:
for D in {yester,to,sun,mon,tues,wednes,thurs,fri,satur}day; 
do printf "%10s:  %s  %s  %s  %s\n" $D \
 `date -d "last $D" +"%F"` \
 `date -d "$D" +"%F"`      \
 `date -d "next $D" +"%F"` \
 `date -d "$D week" +"%F"`; 
done
 
Old 03-31-2011, 01:23 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
Yeah, I know. Most people overlook the power of date. I have written fairly complicated Bash scripts using date and time and date is invariably your friend.

However, a few weeks ago someone asked to find the last Sunday in March and the last Sunday in October. That was something date could not do.

jlinkels
 
Old 03-31-2011, 03:07 PM   #10
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,228

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
A better Perl function is:

#!/usr/bin/perl
use Date::Calc qw(Today Add_Delta_Days);

my ($y,$m,$d) = Add_Delta_Days(Today(),-1);
print "Yesterday was $d-$m-$y\n"'
 
1 members found this post helpful.
Old 03-31-2011, 06:33 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,331

Rep: Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089Reputation: 8089
Quote:
Originally Posted by smallpond View Post
A better Perl function is:

#!/usr/bin/perl
use Date::Calc qw(Today Add_Delta_Days);

my ($y,$m,$d) = Add_Delta_Days(Today(),-1);
print "Yesterday was $d-$m-$y\n"'
+1 for smallpond. I agree...the Date::Calc function in Perl is awesome. It even works across years, and calculates leap years automatically. So if you ask it what the date was 5 days ago on January 3, it'll tell you Dec 29th, last year.

Last edited by TB0ne; 03-31-2011 at 06:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 06:58 AM
Shell Script for Unix Date ??? ajeetraina Linux - Newbie 11 02-08-2008 01:58 AM
How to get date from unix timestamp ruj.sabya Linux - General 9 03-22-2007 06:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration