Saturday, 16 January 2016

7 Examples All You Need To Understand Date In Unix/Linux

This article will describe about the date command available in Unix/Linux. We can display date in several formats, using different switches, as per the requirement. We will not going to see all the options/switches are available instead we will see how can we derive as much as date formats by using minimum number of switchesLet see date command with the help of examples.

Switches which are used on regular basis:

Date Switches:
%d For date in number format (01,02.. 29/30/31)
%aFor day in abbreviated weekday name (Sun,Mon...Sat)
%AFor day in full weekday name (Sunday,Monday...Saturday)

Month Switches:
%mFor month in MN format (01,02..12)
%bFor month in MON format(Jan,Feb..Dec)
%B For Month in MONTH format (January, February.. December)

Year Switches:
%yFor year in YY format (01,02..12)
%YFor year in YYYY                       

Complete Date Switches:
%D Provides date in dd/mm/yy format date; 
same as %m/%d/%y                                      
%FProvides date in YYYY-MM-DD format
same as %Y-%m-%d

Time Switches:

%Ssecond (00..60)                        
%Mminute (00..59) 
%Ihour (01..12)
%Hhour (00..23)

Complete Time Switch:
%T(Hour, Minte, Sec)

SYNTAX:

date "+[switch][delimiter][switch][delimiter]"

1. Date Without Any Switch Provides Current Date And Time.

$ date
Sun, Jan 17, 2016 10:05:59 AM

2. Display Date In DD-MM-YYYY [OR] DD/MM/YYYY Format

$ date "+%d-%m-%Y"

17-01-2016

$ date "+%d/%m/%Y"
17/01/2016

Note: Whenever you use switches it should be prefixed with + sign(Observe it closely in following examples. (-) sign is used for concatenation, you can use any symbol to get the required output.

3. Display Date In DD-MON-YYYY [OR] DD/MON/YYYY Format

$ date "+%d-%b-%Y"

17-Jan-2016

$ date "+%d/%b/%Y"

17/Jan/2016

4. Display Date In DD-MONTH-YYYY [OR] DD/MONTH/YYYY Format

$ date "+%d-%B-%Y"

17-January-2016

$ date "+%d/%B/%Y"

17/January/2016

5. Display Date In DD-DAY-MONTH-YYYY Format

$ date "+%d-%a-%B-%Y"
17-Sun-January-2016

6. display Date In YYYY-MM-DD Format

By now, we know how to get this format:

$ date "+%Y-%m-%d"
2016-01-17

We can use the same output by using single switch as shown below:

$ date "+%F"

2016-01-17

Note: We have derived the same date format using a single switch %F.  It is advisable to have practice of basic switches to derive any format instead of memorising all the switches. If you observe closely, "+%F" = "+%Y-%m-%d"

7. Display Date With DD-MON-YYYY HOUR:MIN:SEC

We will stick with our basics and will see complete switches later:

$ date "+%d-%b-%Y %I:%M:%S"

17-Jan-2016 10:44:06

We can achieve the same by using only two switches(but format will be mm/dd/yy)

$ date "+%D %T"
01/17/16 10:45:08

$ date "+%D %H:%M:%S"
01/17/16 10:47:34

Conclusion:
Unix/Linux provides number of switches to format data according to your requirement.We can check all the switches available by using (man date) command. It is always good to have knowledge of basic switches so that we can create any format as per our requirement instead of knowing every single switch. We will be able to drive any format if we are aware about the above explained switches...!

Keep Reading, Keep Learning, Keep Sharing....!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...