- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use date command in day to day practical usage
In this article, we will learn about the ‘date’ command in Linux and how to practically use the ‘date’ command in day-to-day usage with some practical examples. The ‘date’ command is used to print or to change the system date and time.
[root@localhost ~]# date [OPTION]... [+FORMAT] [root@localhost ~]# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
The Date Command has the following Usages
We can print date and time on the system
We can display the date and time in the given format.
We can read the date from the file
We can display the Universal time.
We can set the system date and time.
Displaying the Date and Time from a given String.
The below example of the command which will take an input and it will display the given string with an option –date.
[root@localhost ~]# date --date=”6/15/2016” Wed Jun 15 15:36:30 IST 2016
Read the date from the file using the ‘-file’ option. This command will read the dates in the file contents and display the dates. For example, if we have a log file and we want to display the date in the log file, we can use this command.
Also, we can create a file and keep some dates in that file so that this command will read the file and display the dates in the file as output.
I have created a file with the below data –
[root@localhost ~]# vi samplelog.txt File contents 04 jun 1978 03 aug 1980 24 feb 2004 22 dec 2007
[root@localhost ~]# date --file=samplelog.txt The output will be Sat Jun 24 00:00:00 IST 1978 Sat Aug 30 00:00:00 IST 1980 Sat Feb 14 00:00:00 IST 2004 Tue Dec 11 00:00:00 IST 2007
Displaying the Past Date
The below command and option will be used to print the passed date. For instance, if we want yesterday or 1 month back or 1 year back.
[root@localhost ~]# date --date='1 day ago' Tue Jun 14 16:01:31 IST 2016 [root@localhost ~]# date --date='yesterday' Tue Jun 14 16:03:01 IST 2016 [root@localhost ~]# date --date='1 month ago' Sun May 15 16:03:35 IST 2016 [root@localhost ~]# date --date='1 year ago' Mon Jun 15 16:03:57 IST 2015
Changing the System Date & Time using the ‘-s’ option
The ‘-s’ is the option for the date command to set the date and time.
$ date -s "Tue June 15 04:08:37 IST 2016" Wed Jun 15 04:08:37 IST 2016
Printing the Universal Time by using the Option –u
This is called the Universal Time (UT) which is like a standard, based on the Earth rotations – The GMT (Greenwich Mean Time).
[root@localhost ~]# date Wed Jun 15 04:10:29 IST 2016 [root@localhost ~]# date -u Tue Jun 14 22:40:31 UTC 2016
Displaying the File Modified Time Stamp using –r Option
We can also verify the date of the file modified using the date with –r option
[root@localhost ~]# date -r samplelog.txt Wed Jun 15 15:45:37 IST 2016
Converting Given Date & Time to Our Time Zone
We can use the date command to convert the date to our or current time zone using this option –d with date command.
[root@localhost ~]# date -d '2016-05-20 18:00 CST' Sat May 21 05:30:00 IST 2016
The other options you can check with the date command are listed below
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 ( 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 timezone (e.g., -0400) %:z +hh:mm numeric timezone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) 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.
By using the date command with many more options, we can find out the date and time of the file created, and date and time in the log files or files in a different format. However, we can convert the date and time from one zone to another as per our requirement.
- How add 1 day to Date in swift?
- How to add a day to the date in MySQL?
- How to add 1 day to the date in MySQL?
- How to get day of month, day of year and day of week in android using offset date time API class?
- How to display first day and last day of the month from date records in MySQL?
- How to update only day portion of MySQL Date?
- How to add 1 day to a Date in iOS/iPhone?
- Format MySQL date and convert to year-month-day
- C++ Program to print current Day, Date and Time
- In MYSQL, how can we store a date where the day, month or both month & day are zero? day are zero?
- Convert day of year to day of month in Java
- How to get a Date from year, month and day in Java?