date
print or set the system date and time
Synopsis
date
[OPTION]... [+FORMAT]
date [-u|--utc|--universal]
[MMDDhhmm[[CC]YY][.ss]]
add an example, a script, a trick and tips
examples
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date=’@2147483647’
Show the time on the west coast of the US (use tzselect(1) to
find TZ)
$ TZ=’America/Los_Angeles’ date
Show the local time for 9AM next Friday on the west coast of the
US
$ date --date=’TZ="America/Los_Angeles" 09:00 next Fri’
date
##What does it do ?
It displays the system's current date, time, time standard and year
##Ouput
Wed Dec 25 13:42:54 GMT 2013
example added by an anonymous user
source
How to insert the date into vim
I kept experimenting till I figured out that vim was expanding
the "%" character. So just escape "\%" and every thing works as I
expected.
:r!date "+\%F"
2012-07-20
Now I can put dates into files Like I would like to
:r!date "+\%F" -d "-2 day"
2012-07-18
source
Store the output of date and watch command to a file
watch
is meant for output to a display. If you
simply want to run a command every X seconds then you should just
use a delay loop for that.
while true ; do somecommand ; sleep 2 ; done
source
How to set current time on Linux?
Check your timezone. hwclock
may return GMT time,
whileas date returns local time, afaik.
source
How to convert from day of year and year to a date YYYYMMDD?
Can't be done in just Bash, but if you have Perl:
use POSIX;
my ($jday, $year) = (100, 2011);
# Unix time in seconds since Jan 1st 1970
my $time = mktime(0,0,0, $jday, 0, $year-1900);
# same thing as a list that we can use for date/time formatting
my @tm = localtime $time;
my $yyyymmdd = strftime "%Y%m%d", @tm;
source
Timezone conversion by command line
This example is from http://www.pixelbeat.org/cmdline.html#dates
It gives the local time corresponding to 9AM on the west coast of
the US, accounting for differing day light savings transitions.
date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Use tzselect to get the TZ. The PST format is ambiguous. IST =
Indian Standard Time and Irish Summer Time for example.
source
How to make an application detect if system time has changed in Linux
I think this article has an answer to your question: Notify
userspace about time changes. But please note that the patch
mentioned in the article is quite recent, so you have to check
your linux kenel vesrion first.
If your kernel does not support userspace notification
mechanism, then you can implement the following algorithm (in
pseudocode):
time = gettimeofday()
loop:
sleep 1 second
new_time = gettimeofday()
if (time_diff(new_time, time) > 2 seconds) then
alert System time has changed by an external user/process!
time = new_time
goto loop
Hope this helps.
source
How do I get the current date according to an NTP server without setting it locally?
This Perl script should do what you need (assuming you don't need
precision to the 10-6 of a second):
#!/usr/bin/perl -w
use strict;
use Math::Round;
## Get current date (epoch)
my $date=time();
## Get the seconds offset, rounding to the nearest second
my $ntp=nearest(0.1,`ntpdate -q $ARGV[0] | gawk '(\$NF~/sec/){print \$(NF-1)}'`);
## Get the server's time
my $ntp_date=$date+$ntp;
## Convert to human readable and print
print "The time according to server $ARGV[0] is " . localtime($ntp_date) . "\n";
Save the script as check_ntp.pl and run it with the server as an
argument:
perl ./check_ntp.pl my.ntp.server
source
Set relative time using command line
You can change your time zone by doing # dpkg-reconfigure
tzdata
. I'm not sure how you would change the time by an
arbitrary value, though.
source
How to set-up date for cron weekly backup?
Here's an idea: Every time a backup is run, the backup script
records the current date by touch
ing a file. The
next time the backup runs, it uses the previous backup's
datestamp file as the argument to tar -N
. For
example, in a shell script, the logic could look like this:
# Begin the latest backup run now.
touch /backups/new-backup-time
tar -N /backups/prev-backup-time -c ... ...
# Save the latest backup time for the next run.
mv /backups/new-backup-time /backups/prev-backup-time
This depends on the fact that the tar -N
option can
either be a date, OR it can be the pathname of a file whose data
modification time specifies the date.
source
How to change the date of Linux to UTC/GMT +1?
You need to change the timezone.
-
To do it system-wide, symlink /etc/localtime
to
the apropriate file in /usr/share/zoneinfo
. For
example:
ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime
In CentOS, you might need to also edit
/etc/sysconfig/clock
.
-
To change the timezone just for the current user, set $TZ
instead:
export TZ="Europe/Paris"
Note that date -u
must always
return correct UTC time.
source
how to change the time format in Linux system
cp /usr/share/zoneinfo/Asia/Calcutta /etc/localtime
or
cp /usr/share/zoneinfo/Asia/Colombo /etc/localtime
Some distribution provide utilities for that like
dpkg-reconfigure tzdata
, tzselect
, etc.
source
On the command line, 'find' reports back an illegal time value
The problem is that ` marks are used to denote commands who's
output should be substituted in your command... so your command
is actually three commands:
- "echo " (contained in the first set of 1 marks)
- "-1308741881 | bc" (contained in the second set of 1 marks)
- find . -mtime -OUTPUT FROM COMMAND #1date +%sOUTPUT
FROM COMMAND #2
Command #1 outputs nothing, and command #2 results in the
"Command not found" error, because -1308741881
is not a valid command, then outputs nothing either.
Then finally the third command runs, with those replacements and
looks like this:
find . -mtime -date
And since "-date" isn't a valid time, it complains about that,
too, saying "illegal time value"
The underlying problem is that you're trying to use nested ``
marks, which the shell interprets as two separate commands.
A better way to express what you want is this (for bash):
find . -mtime -$(echo $(date +%s-1308741881) | bc)s
And for csh:
set now=`date +%s-1308741881`; set date=`echo $now | bc`s; find . -mtime -$date
And possibly for other shells (untested):
NOW=`date +%s-1308741881`; DATE=`echo $NOW | bc`; find . -mtime -${DATE}s
P.S. I don't think this does what you expect... your date command
is returning a number of seconds, but -mtime expects a number of
days as input. I'm guessing you'll probably want to adjust your
date command accordingly.
source
How to Change Server Date?
Use the following syntax to set new data and time:
date --set="STRING"
source
Proper date -d "next monday"
Are you sure there isn't a problem with how you are executing the
command within the script, maybe missing quotes or something of
that nature? I cannot reproduce your problem.
$ date
Tue Jul 31 19:08:50 EDT 2012
$ date -d "next tuesday"
Tue Aug 7 00:00:00 EDT 2012
$ date -d "this tuesday"
Tue Jul 31 00:00:00 EDT 2012
$ date --version
date (GNU coreutils) 8.5
description
Display the
current time in the given FORMAT, or set the system date.
-d, --date=STRING
display time described by
STRING, not ’now’
-f,
--file=DATEFILE
like --date
once for each line of DATEFILE
-I[TIMESPEC],
--iso-8601[=TIMESPEC]
output date/time in ISO 8601
format. TIMESPEC=’date’ for date only (the
default), ’hours’, ’minutes’,
’seconds’, or ’ns’ for date and time
to the indicated precision.
-r,
--reference=FILE
display the last modification
time of FILE
-R,
--rfc-2822
output date and time in RFC
2822 format. Example: Mon, 07 Aug 2006 12:34:56
-0600
--rfc-3339=TIMESPEC
output date and time in RFC
3339 format. TIMESPEC=’date’,
’seconds’, or ’ns’ for date and time
to the indicated precision. Date and time components are
separated by a single space: 2006-08-07
12:34:56-06:00
-s,
--set=STRING
set time described by
STRING
-u,
--utc, --universal
print or set Coordinated
Universal Time
--help
display this help and exit
--version
output version information and
exit
FORMAT controls
the output. Interpreted sequences are:
%%
a literal %
%a
locale’s abbreviated weekday name (e.g., Sun)
%A
locale’s full weekday name (e.g., Sunday)
%b
locale’s abbreviated month name (e.g., Jan)
%B
locale’s full month name (e.g., January)
%c
locale’s date and time (e.g., Thu Mar 3 23:05:25
2005)
%C
century; like %Y, except omit last two digits (e.g.,
20)
%d
day of month (e.g., 01)
%D
date; same as %m/%d/%y
%e
day of month, space padded; same as %_d
%F
full date; same as %Y-%m-%d
%g
last two digits of year of ISO week number (see %G)
%G
year of ISO week number (see %V); normally useful only
with %V
%h
same as %b
%H
hour (00..23)
%I
hour (01..12)
%j
day of year (001..366)
%k
hour, space padded ( 0..23); same as %_H
%l
hour, space padded ( 1..12); same as %_I
%m
month (01..12)
%M
minute (00..59)
%n
a newline
%N
nanoseconds (000000000..999999999)
%p
locale’s equivalent of either AM or PM; blank if
not known
%P
like %p, but lower case
%r
locale’s 12-hour clock time (e.g., 11:11:04
PM)
%R
24-hour hour and minute; same as %H:%M
%s
seconds since 1970-01-01 00:00:00 UTC
%S
second (00..60)
%t
a tab
%T
time; same as %H:%M:%S
%u
day of week (1..7); 1 is Monday
%U
week number of year, with Sunday as first day of week
(00..53)
%V
ISO week number, with Monday as first day of week
(01..53)
%w
day of week (0..6); 0 is Sunday
%W
week number of year, with Monday as first day of week
(00..53)
%x
locale’s date representation (e.g., 12/31/99)
%X
locale’s time representation (e.g., 23:13:48)
%y
last two digits of year (00..99)
%Y
year
%z
+hhmm numeric time zone (e.g., -0400)
%:z
+hh:mm numeric time zone (e.g., -04:00)
%::z
+hh:mm:ss numeric time zone (e.g.,
-04:00:00)
%:::z
numeric time zone with : to necessary precision (e.g.,
-04, +05:30)
%Z
alphabetic time zone abbreviation (e.g., EDT)
By default,
date pads numeric fields with zeroes. The following optional
flags may follow ’%’:
-
(hyphen) do not pad the
field
_
(underscore) pad with spaces
0
(zero) pad with zeros
^
use upper case if possible
#
use opposite case if possible
After any flags
comes an optional field width, as a decimal number; then an
optional modifier, which is either E to use the
locale’s alternate representations if available, or O
to use the locale’s alternate numeric symbols if
available.
copyright
Copyright © 2012 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute
it. There is NO WARRANTY, to the extent permitted by law.
date string
The --date=STRING is a mostly free format human readable date
string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
16:21:42" or even "next Thursday". A date string may contain
items indicating calendar date, time of day, time zone, day of
week, relative time, relative date, and numbers. An empty string
indicates the beginning of the day. The date string format is
more complex than is easily documented here but is fully
described in the info documentation.
reporting bugs
Report date bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page:
<http://www.gnu.org/software/coreutils/>
General help using GNU software:
<http://www.gnu.org/gethelp/>
Report date translation bugs to
<http://translationproject.org/team/>
see also
The full
documentation for date is maintained as a Texinfo
manual. If the info and date programs are
properly installed at your site, the command
info
coreutils 'date invocation'
should give you
access to the complete manual.
author
Written by
David MacKenzie.