Home » 2015 » April » 23 » 15 Great DATE command examples

7:26 PM
15 Great DATE command examples

Appear for online test on Date and system related QUIZ

The best way to understand date and time of the system is to explore several examples. So, we are exercising some common date commands here:

date and time

1.To print the date of the day before yesterday:

          date --date='2 days ago'

Prompt:>date -d '2 days ago'
Tue Apr 21 13:55:20 CEST 2015
Prompt:>date --date='2 days ago'
Tue Apr 21 13:55:39 CEST 2015


2.Set the system clock forwared by four minutes

    date --set='+4 minutes'

  3.To print the date of the day four months and two days later:

          date --date='4 months 2 days'

Prompt:>date -d '3 months 4 days'
Mon Jul 27 13:56:38 CEST 2015

4. To print the day of Christmas in the current year:

          date --date='25 Dec' +%j

Prompt:>date -d '25 Dec'
Fri Dec 25 00:00:00 CET 2015
Prompt:>date --date='25 Apr'
Sat Apr 25 00:00:00 CEST 2015


5. Create a backup of file with current system timestamp

Using date command with the formats(%d,%m, %y, %s ....), we can take a backup of the file as shown below:

Prompt>cp -pv testfile testfile.`date +%Y%m%d%H%M`
`testfile' -> `testfile.1504241657'

ltkwvpsx02:/home/qquniq:>cp -pv testfile testfile.$(date +%Y%m%d%H%M)
`testfile' -> `testfile.201504241700'

6.To print the current full month name and the day of the month:

          date '+%B %d'

Prompt:>date '+%B %d'
April 23

But lets say if we want to remove extra zero from date 01 to 09, we can use '-'flag.  For example

`date -d 1apr '+%B %d''

will print `Apr 01'.

Prompt:>date -d 1apr '+%B %d'
Apr 01

7.To print a date without the leading zero for one-digit days of the  month, we can use the (GNU extension) `-' flag to suppress the padding:

          date -d 1may '+%B %-d

Prompt:>date -d 1apr '+%B %-d'
Apr 1


 8.To print the current date and time in the format required by many non-GNU versions of `date' when setting the system clock:

          date +%m%d%H%M%Y.%S

Prompt:>date +%m%d%H%M%Y.%S

9. To set the system clock forward by two minutes:

          date --set='+2 minutes'

Prompt:>date --set='+2 minutes'
date: cannot set date: Operation not permitted
Thu Apr 23 16:24:53 CEST 2015

You must be a priviledged user to change date setting of the system.

10. To print the date in RFC 2822 format, use `date --rfc-2822'.

Prompt:>date --rfc-2822
Thu, 23 Apr 2015 16:24:13 +0200

11. To convert a date string to the number of seconds since the epoch
     Epoch:1970-01-01 00:00:00 UTC),

We can use the `--date' option with the `%s' format.
 That can be useful in sorting and/or graphing and/or comparing data by date. 

The command to print number of seconds two minutes after epoch.

date --date='1970-01-01 00:02:00 +0000' +%s


12. Print the system time zone of the system

Prompt:>date +%Z

13. To check the last modification date and time of a file.

date -r filename

Promt> date -r files.pk
Fri Jun 29 17:31:59 CEST 2012


14. To print the date of yesterday or tomorrow

Prompt:>date -d tomorrow
Sat Apr 25 17:48:11 CEST 2015
Prompt:>date -d yesterday
Thu Apr 23 17:48:21 CEST 2015 

15. To print the universal time (UTC) 

Prompt:>date -u
Fri Apr 24 15:52:28 UTC 2015


FORMAT supported by date comand for different output

       %%     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 ( 0..23)

       %l     hour ( 1..12)

       %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 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

Appear for online test on Date and system related QUIZ



Category: Open System-Linux | Views: 1280 | Added by: shanky | Tags: time zone, timestamp, Time, unix, Date, system date and time, linux | Rating: 0.0/0

Related blogs

You may also like to see:

[2015-06-19][Open System-Linux]
How to create swap file in Linux : Adding extra swap space
[2016-03-17][Open System-Linux]
Some amazing command shortcuts in Linux
[2014-10-06][Open System-Linux]
Time command in Linux to give resource usage
[2014-08-31][Open System-Linux]
20 command line tools to check load and performance of a Linux System
[2016-04-14][Open System-Linux]
AT command in Linux : schedule a task at later time

Total comments: 0