We have moved to www.dataGenX.net, Keep Learning with us.

Tuesday, June 26, 2012

date commands


A useful command line tool is date, which is typically used for displaying the current system date, or setting it. The default format of the date and time displayed will be the system default, eg "date : Wed May  9 19:42:23 GMT 2012", but it is possible to apply your own formatting, and also to specify a different date to use, without adjusting the system clock.

The date command writes the current date and time to standard output if called with no flags or with a flag list that begins with a + (plus sign). Otherwise, it sets the current date. Only a root user can change the date and time.
If, for example, you wanted to display the current day's day of the week only.
If you follow the date command with a + (plus sign) and a field descriptor, you can control the output of the command. You must precede each field descriptor with a % (percent sign). The system replaces the field descriptor with the specified value. Enter a literal % as %% (two percent signs). The date command copies any other characters to the output without change. The date command always ends the string with a new-line character.

date +%A

The + switch tells the date command to apply the following format to the current date. %A tells date that the format to use is the locale's full weekday name. A full list of the formatting modifiers is at the end of this article. It's Friday today, so entering the above command at the command prompt would display this:

$ date +%A
Wednesday

If you wanted to display the date in the format YYYY-MM-DD, with a 4 digit year and 2 digit months with leading zeros, you would do this:

$ date +%Y-%m-%d
2012-05-09


Specifying different dates

That was pretty easy, but the above examples only show the current system date. What if you wanted to show yesterday's date? There's another switch for date which allows you to specify a date other than the current one, the -d switch. The great thing with -d is you can use words to specify previous or future dates, as per the examples below.


Using date in other commands

Within the bash/ksh shell you can embed commands within other commands using backticks. As a very simple example, we'll use the echo command. The first example is without backticks so will just echo the word "date" the second example uses backticks and does echo the date. You wouldn't normally do this because date echoes the output anyway.

$ echo date
date

$ echo `date`
Wed May 9 19:42:23 GMT 2012


Date format specifies

The following are the available date format specifiers: (Some are supported by only specific shells, please check before using these)

%%     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., 21)
%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     alphabetic time zone abbreviation (e.g., EDT)



njoy the simplicity.......
Atul Singh

2 comments :

  1. How to convert the date to unix epoch timestamp?

    Date command examples in unix

    ReplyDelete
    Replies
    1. use this command to change the current date to unix epoch time
      date +"%s"

      Delete