19 March, 2014

date (Unix)


Formatting

To format a date, a parameter string beginning with a plus sign (+) is given. The formatting specifiers below are supported by most implementations.


Format specifiers (format string starts with +)
SpecifierDescriptionValues or example
Day
%aweekday, abbreviatedWed
%Aweekday, fullWednesday
%dday of the month, two digits, zero filled08
%eday of the month8
%jday of year, zero filled001–366
%uday of week from Monday to Sunday1–7
%wday of week from Sunday to Saturday0–6
Week
%Uweek number, Sunday as first day of week00–53
%Wweek number, Monday as first day of week00–53
%VISO standard week of the year01–53
Month
%mtwo-digit month number01–12
%hmonth name, abbreviatedMar
%bmonth name, localised abbreviationMar
%Blocale's full month, variable lengthMarch
Year
%ytwo-digit year00–99
%Yfour-digit year2014
%gtwo-digit year corresponding to the %V week number
%Gfour-digit year corresponding to the %V week number
Century
%Ctwo century digits from year00–99
Date
%Dmm/dd/yy03/19/14
%xlocale's date representation03/19/2014
%F%Y-%m-%d2014-03-19
Hours
%lhour (12 hour)10
%Ihour (12 hour), zero-filled10
%khour (24 hour)10
%Hhour (24 hour), zero-padded10
%plocale's upper case AM or PM (blank in many locales)AM
%Plocale's lower case am or pmam
Minutes
%Mtwo-digit minute number52
Seconds
%sseconds since 00:00:00 1970-01-01 UTC (Unix epoch)1395226379
%Stwo-digit second number00–60 (Includes 60 to accommodate a leap second)
%Nnanoseconds000000000–999999999
Time
%rhours, minutes, seconds (12-hour clock)10:52:59 AM
%Rhours, minutes (24 hour clock)10:52
%Thours, minutes, seconds (24-hour clock)10:52:59
%Xlocale's time representation11:07:26 AM
Date and time
%clocale's date and timeSat Nov 04 12:02:33 EST 1989
Time zone
%zRFC-822 style numeric time zone-0500
%Ztime zone name; nothing if no time zone is determinableEST, EDT
literals: %n newline      %% percent      %t horizontal tab
By default, date normally fills numeric fields with zeroes. GNU date, but not BSD date, recognizes a modifier between the per cent sign (%) and the format specifier:
  • hyphen (-): do not fill the field
  • underscore (_): pad the field with spaces
TZ Specifies the time zone, unless overridden by command line parameters. If neither is specified, the setting from /etc/localtime is used.

example
$ date "+%m/%d/%y" 
7/4/06
$ date "+%Y%m%d"
20060704

To assign the time to a variable
 $ START=`date '+%r'`
 $ echo $START
 03:06:02 PM
 $ sleep 5
 $ echo $START
 03:06:02 PM
N.B. the variable has the time when it was assigned.
Yesterday assigned to variable
 $ DATE=$(date -d yesterday +"%Y%m%d")
 $ echo $DATE
 20060704
The TZ environment variable specifies the time zone. Valid values are in /usr/share/zoneinfo
 $ TZ=GMT; echo "GMT: `date +\"%R (%Z)\"`"
 GMT: 12:30 (GMT)
 $ TZ=Europe/Stockholm; echo "Stockholm: `date +\"%R (%Z)\"`"
 Stockholm: 13:30 (CET)
 $ TZ=Asia/Kuala_Lumpur; echo "Kuala Lumpur: `date +\"%R (%Z)\"`"
 Kuala Lumpur: 20:30 (MYT)
 $ TZ=US/Central; echo "Dallas: `date +\"%R (%Z)\"`"
 Dallas: 07:30 (CDT)
Converting between time zones Example: What time is it in Moscow when it will be 17:35 in Los Angeles
 $ TZ=Europe/Moscow date "+%F %R (%Z%z)" -d 'TZ="America/Los_Angeles" 17:35'
 2013-03-22 04:35 (MSK+0400)
Other valid time strings
GNU dateBSD dateoutput
$ date +"%Y%m%d" -d sunday$ date -v +sun +"%Y%m%d"20060709
$ date +"%Y%m%d" -d last-sunday$ date -v -sun +"%Y%m%d"20060702
$ date +"%Y%m%d" -d last-week$ date -v -1w +"%Y%m%d"20060627
$ date +"%Y%m%d" -d last-month$ date -v -1m +"%Y%m%d"20060604
$ date +"%Y%m%d" -d last-year$ date -v -1y +"%Y%m%d"20050704
$ date +"%Y%m%d" -d next-week$ date -v 1w +"%Y%m%d"20060711
$ date +"%Y%m%d" -d next-month$ date -v 1m +"%Y%m%d"20060804
$ date +"%Y%m%d" -d next-year$ date -v 1y +"%Y%m%d"20070704
$ date +"%Y%m%d" -d "2 days ago"$ date -v -2d +"%Y%m%d"20060702
$ date +"%Y%m%d" -d "2 months ago"$ date -v -2m +"%Y%m%d"20060504
$ date +"%Y%m%d" -d "2 years ago"$ date -v -2y +"%Y%m%d"20040704
To show the time in seconds since 1970-01-01 (Unix epoch):
$ date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879
To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
$ date -d "UTC 1970-01-01 1240596879 secs"
Fri Apr 24 13:14:39 CDT 2009
Or:
$ date -ud @1000000000
Sun Sep  9 01:46:40 UTC 2001

1 comment:

  1. Roulette - The Game of Casino Roulette | Goyangfc
    Roulette. 1 bet Casino. Roulette. Roulette. Casino. The 배팅 Game 알바로 모라타 of Casino 강원랜드여자 Roulette. Casino. The Game of Casino Roulette. Casino. The Game of Casino Roulette. Casino. The Game of 모모벳

    ReplyDelete