Scripting with Unix Date
I have been "playing" with the date command for a while in various Unix shell scripts and found the following date options quite useful.
Setting Unix system date and time
- November 13, 06:30 a.m., 2010 do the following: date 111306302010
Unix epoch time to regular time
- date –d @1289524456 will provide a result of "Thu Nov 11 20:14:16 EST 2010"
Unix date to epoch time
- date +%s -d “2010-11-03” will provide a result of 1288756800
Unix epoch time to print only the time Fri Sep 10 10:00:01 EDT 2010
- date -d @1288310401 +%k:%M will provide a result of 20:00 hours
Print yesterday's date (today - 1) in the Year-Month-Day format
- date --date "-1 days" +"%Y-%m-%d" will produce a result of 2010-11-10
Print last month's date (today - 1 month) in the Year-Month-Day format
- date --date "-1 month" +"%Y-%m-%d" will produce a result of 2010-10-11
You can also check the Unix man pages to display other time/date combination. If you know other date "tricks" you would like to share, you can send them via our contact page and I will added them to this diary.
-----------
Guy Bruneau IPSS Inc. gbruneau at isc dot sans dot org
Keywords: Unix Date
11 comment(s)
×
Diary Archives
Comments
date --date "-1 days" +"%Y-%m-%d"
try
date --date "yesterday" +"%Y-%m-%d"
It also understands "now" and "today".
Jeremy Impson
Nov 12th 2010
1 decade ago
date --date "Thu Nov 11 21:23:34 PST 2010"
Fri Nov 12 00:23:34 EST 2010
Jeremy Impson
Nov 12th 2010
1 decade ago
What is 1234567890.123 equal to in real life. What is the conversion algorythem?
24/60/365 +1900 almost works, but I'm missing something.
Mikel
Nov 12th 2010
1 decade ago
That matters a great deal with 'date' because the variations are many and they are sometimes dangerously incompatible. For example, the "-d" option with a non-zero argument in some BSD versions of 'date' sets the kernel DST flag. Because of those incompatibilities, if you write a script using 'date' that may end up on a system different from the one you wrote the script for, you should check which 'date' you have. If you want GNU date only, you can *probably* trust that it is the only variant that will accept "--version" as an option.
Bill Cole
Nov 12th 2010
1 decade ago
That matters a great deal with 'date' because the variations are many and they are sometimes dangerously incompatible. For example, the "-d" option with a non-zero argument in some BSD versions of 'date' sets the kernel DST flag. Because of those incompatibilities, if you write a script using 'date' that may end up on a system different from the one you wrote the script for, you should check which 'date' you have. If you want GNU date only, you can *probably* trust that it is the only variant that will accept "--version" as an option.
Bill Cole
Nov 12th 2010
1 decade ago
However, most 10-digit numbers starting with '12' for times near the present are "Unix epoch" times, a count of seconds since 1970-01-01 00:00:00 UTC. Because it is actually quite complex to calculate that arithmetically (particularly given leap seconds...) most conversions from epoch to human use 'date' or similar tools and the system's collection of timezone and calendar data. To convert epoch to the default string format using FreeBSD/MacOS date:
$ date -j -f %s 1234567890
Fri Feb 13 18:31:30 EST 2009
GNU/Linux date:
$ date --date=@1234567890
Fri Feb 13 18:31:30 EST 2009
A 13-digit number or <10-digit>.<3-digit> is often composed of an epoch time and a millisecond count.
Bill Cole
Nov 12th 2010
1 decade ago
Maanus
Nov 12th 2010
1 decade ago
"date --date='2 weeks ago'" +%Y.%m.%d
but "date --date='-2 weeks'" +%Y.%m.%d would work just as well.
Funtime
Nov 12th 2010
1 decade ago
Funtime
Nov 12th 2010
1 decade ago
date --date "-1 days" +"%F"
Joshua Gimer
Nov 13th 2010
1 decade ago