Adjusting Dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adjusting Dates
# 1  
Old 01-29-2011
Adjusting Dates

What is the easiest way to find the date 6 month prior to the current date.

Example:
Today is 2011/01/29
I need to find the 1st day of the month, 6 month ago, which is 2010/08/01. I have to count 1/1/2011 as a previous month, since the current day is past 1/1/2011. Is there any easy function???

Thanks.
# 2  
Old 01-29-2011
with GNU date
Code:
date -d '6 months ago' '+%Y/%m/01'

# 3  
Old 01-29-2011
Doesn't look like I have GNU Date available, is there another option???
Thanks.
# 4  
Old 01-29-2011
Hi,

Test this solution using 'perl'. I had to install the 'DateTime' module using 'cpan'. Here my console session:
Code:
$ su -
# cpan
cpan> install DateTime
cpan> exit
# exit
$ cat script.pl
use strict;
use warnings;
use DateTime;

die "Usage: $0 [#months]\n" unless @ARGV && $ARGV[0] =~ /^\d+$/;

my $monthsBefore = $ARGV[0];

my $dt = DateTime->today();
$dt->set_day(1), print($dt->ymd('/'), "\n"), exit if $monthsBefore == 0;

my $cdt = $dt->clone();
$dt->set_day(1);

my $cmp = DateTime->compare($dt, $cdt);
--$monthsBefore if $cmp < 0;
$dt->subtract(months => $monthsBefore);
print $dt->ymd('/'), "\n";
$ perl script.pl 6
2010/08/01

Regards,
Birei
# 5  
Old 01-29-2011
Quote:
Originally Posted by jclanc8
Doesn't look like I have GNU Date available, is there another option???
Thanks.
At your shell command prompt typing in
Code:
date

does nothing? I have yet to see a distro that didn't come with date. Odd...
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adjusting my awk output format

I've been diving into awk but still learning how to use it for text formatting. Below you can see my results are separated by a comma. Can somebody show me how to separate by TAB as well? An explanation would be appreciated as I did not comprehend the answer in the man pages and would like to gain... (1 Reply)
Discussion started by: sudo
1 Replies

2. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

3. UNIX for Dummies Questions & Answers

Problem adjusting the output

Hi i m running a command watch -n 1 -d netstat -i to see the packet drops every 1 second. but the problem is the output is so long(Due to large number of virtual interfaces) it doesn't fit into the putty prompt. I dont need to monitor each and every network interface I m more interested in... (5 Replies)
Discussion started by: pinga123
5 Replies

4. Programming

adjusting a code for Linuxconio.h

Hi All, I am using a book to learn C++. Unfortunately the book, sometimes, uses the maddening phrase, #include <conio.h>, which is a M$ related file and not a part of STL, in some of its examples. My question is: How would you modify the following code so it would compile and run on Linux?... (3 Replies)
Discussion started by: eager2no
3 Replies

5. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

6. Solaris

Setting timezone Sunos 5.8 & adjusting

I am new to this so i figure this is an easy one.. How do i change the time zone from Central to Eastern time in SunOS 5.8 ? I thought I needed to edit the /etc/TIMEZONE and them issue TZ=US/Eastern but I want to check. Is a reboot required afterwards? If I want to change the system time... (3 Replies)
Discussion started by: jay6ird
3 Replies

7. Shell Programming and Scripting

Adjusting the output in a file

QUERY SCENARIO Here is the actual scenario LOOP echo "$COLNAME $TYPENAME($LENGTH) $NULLS ">>$DDL_FILE END-LOOP COLNAME, TYPENAME, LENGTH, NULLS are the variables and within echo statment the output of which has to go into file specified by DDL_FILE. ... (1 Reply)
Discussion started by: skyineyes
1 Replies
Login or Register to Ask a Question