Subtract 2 days from timestamp


 
Thread Tools Search this Thread
Operating Systems Solaris Subtract 2 days from timestamp
# 1  
Old 09-14-2016
Subtract 2 days from timestamp

Hi,

I am running on Solaris 11.3.

I have a timestamp in this format date +%Y%m%d%H%M . [ex: 201609140905]
I found some forums saying to use date --d however that didn't work.

Is there a way how I can subtract days from timestamp in that format?

Thanks

Last edited by vbe; 09-14-2016 at 08:44 AM..
# 2  
Old 09-14-2016
Hi.

There may be a GNU date on your system:
Code:
$ which gdate
/usr/bin/gdate
$ gdate   
Wed Sep 14 07:48:45 CDT 2016

However, you could also use ksh:
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate date arithmetic with ksh printf.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
# C=$HOME/bin/context && [ -f $C ] && . $C
version =o version
echo ksh - ${.sh.version}

# %Y%m%d%H%M . [ex: 201609140905]
pl " Results, today:"
printf "%(%Y%m%d%H%M)T\n" now

pl " Results, 2 days ago:"
printf "%(%Y%m%d%H%M)T\n" "2 days ago"

pl " Results, almost any time:"
arbitrary_date=201409140905
printf "%(%Y%m%d%H%M)T\n" "$arbitrary_date"

pl " Results, 2 days before almost anytime:"
printf "%(%Y%m%d%H%M)T\n" "$arbitrary_date 2 days ago"

exit 0

producing:
Code:
$ ./s1
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
version (local) 1.77
ksh - Version JM 93u 2011-02-08

-----
 Results, today:
201609140747

-----
 Results, 2 days ago:
201609120747

-----
 Results, almost any time:
201409140905

-----
 Results, 2 days before almost anytime:
201409120905

See man pages or Google search results for details.

Best wishes ... cheers, drl
# 3  
Old 09-14-2016
can you try

Code:
$ date --date="2 day ago"

# 4  
Old 09-15-2016
Quote:
Originally Posted by chakrapani
can you try

Code:
$ date --date="2 day ago"

Code:
date --date...

is a non-standard GNU extension.

The current POSIX standard for the date utility is here: date
# 5  
Old 09-16-2016
Quote:
Originally Posted by nms
Hi,

I am running on Solaris 11.3.

I have a timestamp in this format date +%Y%m%d%H%M . [ex: 201609140905]
I found some forums saying to use date --d however that didn't work.

Is there a way how I can subtract days from timestamp in that format?

Thanks
It'd be a breeze if you know perl...
# 6  
Old 09-16-2016
Quote:
Originally Posted by nms
Hi,

I am running on Solaris 11.3.

I have a timestamp in this format date +%Y%m%d%H%M . [ex: 201609140905]
I found some forums saying to use date --d however that didn't work.

Is there a way how I can subtract days from timestamp in that format?

Thanks
Modern solaris ought to have GNU date somewhere, either as gdate or buried under /usr/gnu/bin/ or something but a lot of installers seem to have bypassed it somehow.

If you have perl, you can use my generic date script which emulates some of GNU date's -d feature.
# 7  
Old 09-16-2016
Pitchin' in my 2 cents...
Code:
#!/usr/bin/perl

use warnings;
use Time::Local;
use Time::localtime;

use constant dt => "201609140905";
use constant ti => 2*24*60*60;

$y = substr(dt, 0, 4) - 1900;
$m = substr(dt, 4, 2) - 1;
$d = substr(dt, 6, 2);
$H = substr(dt, 8, 2);
$M = substr(dt, 10, 2);
$S = substr(dt, 12);

$epoch = timelocal($S,$M,$H,$d,$m,$y) - ti;
$tm = localtime($epoch);

printf("%04d%02d%02d",$tm->year+1900,$tm->mon+1,$tm->mday);
printf("%02d%02d\n",$tm->hour,$tm->min);

Save the file and give it a name...for example "my_date_time.pl" and run it on the cmd line as # my_date_time.pl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Get a given date and subtract it to 5 days ago

Hi all, I have been researching to obtain SSL certification expiry for most of our webistes. For some cases, some hosts where not directly accessible so i finally got a solution working with curl using my proxy. This lists the expiry date which i'm finally looking for. # curl --proxy... (4 Replies)
Discussion started by: nms
4 Replies

3. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

4. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

6. Shell Programming and Scripting

Subtract months/days from date

Hi, Can you please let me know code for the below (in korn shell) a) Subtract month(s) from given date b) Subtract day(s) from give date c) Subtract month(s) from given timestamp d) Subtract day(s) from give timestamp (1 Reply)
Discussion started by: tostay2003
1 Replies

7. Shell Programming and Scripting

Subtract 2 date columns in .csv file and get output as number of days

Hi, I have one .csv file. I have 2 date columns present in file, column 2 and column 3. I need to calculate how many days exist between 2 dates. I am trying to subtract date column 2 from date column 3. Eg: my file look likes s.no, Start_date,End_Date 1, 7/29/2012,10/27/2012 2,... (9 Replies)
Discussion started by: Dimple
9 Replies

8. UNIX for Advanced & Expert Users

Subtract days to a date in AIX 5.3

good afternoon, can someone help me, I need to make a script where n subtract days to a date. I am using AIX 5.3. Greetings. (4 Replies)
Discussion started by: systemoper
4 Replies

9. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

10. Shell Programming and Scripting

Subtract days from a variable holding date

Hi, could someone help on this.. I have a date in variable procdate="05/30/2009" I would want to Subtract it with 3 or 4 (2 Replies)
Discussion started by: infernalhell
2 Replies
Login or Register to Ask a Question