Senin, 28 April 2014

10 examples of Date command in Unix




Date command in unix or Linux is one of important command to learn and master because we always need date information. no matter you want to know current date in unix or your bash script needs current date in unix for archiving purpose you need to use date command. In its simplest format date command shows the current date and time in unix while with sophisticated option we can extract many useful information from unix date command. In this Unix command tutorial  we will see some useful tips on using date command in unix and learn more around date in unix and Linux. One important thing to note is that different implementation of unix date command may not be exactly same e.g. AIX version of date command doesn't support option "-d" or "date". Anyway let's see some example of unix date command:





By the way this is fifth article in my series of covering examples of important and frequently used commands e.g. find examples, grep examples, chmod example and sort command example.









1) Example 1 -  displaying current date and time in Unix


This is simplest use of unix date command. just type date in the command prompt and it will show you the current date and time in the timezone your machine is.






~/java date

Thu Jul 14 14:31:35 MPST 2011









2) Example 2 - displaying Julian date in Unix


Date command examples in Unix and Linuxmost of java stock trading application running under Linux use Julian date format to store date information. Julian dates are made of 5 digits 2 of which represent year and 3 or last digit represent day of year. for example 14th July 20111 will be represented by 11195 in Julian format because 14th July is the 195th day of the year. we can get Julian date by using date command in unix as shown in the below example of unix date command:






Converting current date into Julian format in unix

~/java date +%j

195



Converting a specific date into Julian format in unix

~/java date -d "2011/07/13" +%j

194






this way you can get yesterdays Julian date or tomorrow's Julian date in Unix though I have heard that "-d" option of date is not supported in AIX so this may not work in AIX version of Unix.








3) Example 3 - displaying current date in various format in unix


Unix date command is very powerful and can show date in various format. in this section of unix date tutorial we will convert date into some commonly used date format in unix:






Showing date in YYYYMMDD format in Unix

~/java date +%Y%m%d

20110714






here %Y shows year in four digit ( you can use %y (small letter) for displaying just 2 digit of year)  %m  shows two digit of month as 01..12 and %d represent day of month.






Showing date in DD/MM/YYYY format in unix

~/java date +%d\/%m\/%Y

14/07/2011






here again %d is for day %m is for month and %Y is for four digit year if you want to show 2 digit use small case y e.g. %y






Showing date in DD-MM-YY format in unix

~/java date +%d\-%m\-%Y

14-07-2011






quite similar to above approach just chaged the "/" to "-", you can change it to any character as per your wish and until unix accept it.








displaying date in MM/DD/YYYY format in Unix


what do you guys think would it be difficult no right ? just exchange place of "%m" and "%d" and you are ready as shown in below example of date command in unix






~/java date +%m\/%d\/%Y

07/14/2011






displaying date in YYYY-MM-DD format is exactly similar to above approach just change the order of year and month.








4) How to find date 7 days before current date in Unix


This is really nice of gnu version of unix date command you can easily get what's the date 4 days before or 5 days before by using date command in unix. here is example:






date after 7 days in unix

~/java date -d "-7 days"

Thu Jul  7 14:54:36 MPST 2011






by using this way you can find any date which is any days apart from a given current date in unix








5) How to get date 7 days after current day in Unix and Linux


As shown above you can get the date after 7 days in unix by using gnu version of date command in unix, here is an example of getting date 7 days after current date:






~/java date -d "7 days"

Thu Jul 21 14:54:15 MPST 2011






this way you can find any date which is 7 days past of a given current date in unix








6) Setting date time into unix


setting date is easy though I don't recommend to do it if you have unix admins. Also changing date requires root access which you might not have with you anyway we can use unix date command to change the date, just provide new date in the specified format and your current date will be set to new date.





format of setting date time into unix





date mmddhhmi





where mm = month


      dd = day of month


      hh = hour


      mi = minute


  


Example of changing date time in unix






~/java date 07240222

Sun Jul 24 02:22:00 MPST 2011






above example of date command will set the current date to 24th July and time to 02:22, we don't provide year information and date uses current year by default.





7) How to find timezone by using unix date command - Example


unix date command can display timezone related information also. it provides following option to display timezone


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





Now let's see some example of unix date command to display timezone






~/java date +%Z  (this will display timezone in common format e.g. EST, GMT)

MPST



~/java date +%z  (this option of unix date command will display timezone respective of GMT)

+0800









8) How to show day of week in Unix using date command - Exmaple


you can show current date, day of week name using unix date command  by using options "%a" and "%A". small case is used to display day in short format while capital one is used to display full name of day. see the below example of date command in unix for display day of week :






~/java date +%a

Thu



~/java date +%A

Thursday









9) Date command in Unix for showing current time in Various format


time is another important information which we can get from unix date command. by default date command in unix display both date and time information but we can use various options provided by date command to display only time information. let's see some example of showing time using unix date command:





10) Date to command for showing time in format HH:MM:SS


As shown above on showing date in different format in unix you can also display time on different format by applying same approach. you just need to remember following time related options of unix date command





%H   hour (00..23)


%M   minute (00..59)


%S   second (00..60)





that's all for now guys I hope you will be more comfortable with date command in unix after going through and trying these examples. you can get more information about date command in unix by using "man date" or "date --help" which will also list all the options supported by unix date command.





Tip: just remember that unix date command is mostly used inside bash script to get the date information , so its worth mentioning here how to execute date command from bash script, you just need to specify date command in back quotes . see the example bash script  :






~/java cat unixdate.sh

#!/bin/bash



DATE=`date`

echo "Current date: $DATE"



~/java ./unixdate.sh

Current date: Sun Jul 24 02:30:50 MPST 2011






So that was all about different examples of date command. We have seen how we can format date in Unix, how we can convert date into different timezone in Unix, how we can get current date and time inside bash script and several other examples of date command. Let me know if you have any other usage of date command which is not covered here.








Other Linux tutorials from Javarevisited:





























Source:http://javarevisited.blogspot.com/2012/06/10-examples-of-date-command-in-unix.html

Tidak ada komentar:

Posting Komentar