date_now=`date +%s` 可在shell脚本里面赋给变量值
常用参数如下:
%% 输出%符号 %a 当前域的星期缩写 (Sun..Sat) %A 当前域的星期全写 (Sunday..Saturday) %b 当前域的月份缩写(Jan..Dec) %B 当前域的月份全称 (January..December) %c 当前域的默认时间格式 (Sat Nov 04 12:02:33 EST 1989) %C n百年 [00-99] %d 两位的天 (01..31) %D 短时间格式 (mm/dd/yy) %e 短格式天 ( 1..31) %F 文件时间格式 same as %Y-%m-%d %h same as %b %H 24小时制的小时 (00..23) %I 12小时制的小时 (01..12) %j 一年中的第几天 (001..366) %k 短格式24小时制的小时 ( 0..23) %l 短格式12小时制的小时 ( 1..12) %m 双位月份 (01..12) %M 双位分钟 (00..59) %n 换行 %N 十亿分之一秒(000000000..999999999) %p 大写的当前域的上下午指示 (blank in many locales) %P 小写的当前域的上下午指示 (blank in many locales) %r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M) %R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm) %s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数(a GNU extension) %S 双位秒 second (00..60); %t 横向制表位(tab) %T 24小时制时间表示(hh:mm:ss) %u 数字表示的星期(从星期一开始 1-7) %U 一年中的第几周星期天为开始 (00..53) %V 一年中的第几周星期一为开始 (01..53) %w 一周中的第几天 星期天为开始 (0..6) %W 一年中的第几周星期一为开始 (00..53) %x 本地日期格式 (mm/dd/yy) %X 本地时间格式 (%H:%M:%S) %y 两位的年(00..99) %Y 年 (1970…)
如:
在shell中根据时间戳生成文件名:
FILENAME="item_detail_"`date +%Y%m%d%H%M%S`".txt" echo $FILENAME
结果如下:
item_detail_20140507031200.txt
可使用【man date】命令看一下官方说明:
Commands Reference, Volume 2, d - h
date Command
Purpose
Displays or sets the date or time.
Syntax
To Set the Date and Time as Root User
/usr/bin/date [ -n ] [ -u ] [ Date ] [ +FieldDescriptor ... ]
To Display the Date and Time
/usr/bin/date [ -u ] [ +FieldDescriptor ... ]
To adjust the Time in Seconds as root User
/usr/bin/date [ -a [ + | - ]sss[.fff ]
Description
Attention: Do not change the date when the system is running with more than one user.
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. The date command prints out the usage message on
any unrecognized flags or input.
The following formats can be used when setting the date with the Date parameter:
* mmddHHMM[YYyy]
* mmddHHMM[yy]
The variables to the Date parameter are defined as follows:
mm
Specifies the month number.
dd
Specifies the number of the day in the month.
HH
Specifies the hour in the day (using a 24-hour clock).
MM
Specifies the minute number.
YY
Specifies the first two digits of the year. Note: If you do not specify the first two digits of the year, values in the range 69 to 99
refer to the twentieth century, 1969 to 1999 inclusive, and values in the range 00 to 68 refer to years in the twenty-first century,
2000 to 2068 inclusive.
yy
Specifies the last two digits of the year. Note: The date command accepts a 4 digit year as input. For example, if a four-digit year is
specified, the date command tries to set the year to "YYyy" and fails for values which are out of range (less than 1970 and greater than
2037).
The current year is used as the default value when the year is not specified. The system operates in Coordinated Universal Time (CUT).
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.
Flags
-a [ + | - ]sss[.fff ]
Slowly adjusts the time by sss.fff seconds (fff represents fractions of a second). This adjustment can be positive or negative. The
system's clock will be sped up or slowed down until it has drifted by the number of seconds specified.
-n
Does not set the time globally on all machines in a local area network that have their clocks synchronized.
-u
Displays or sets the time in Coordinated Universal Time (CUT).
Field Descriptors
%a
Displays the locale's abbreviated weekday name.
%A
Displays the locale's full weekday name.
%b
Displays the locale's abbreviated month name.
%B
Displays the locale's full month name.
%c
Displays the locale's appropriate date and time representation. This is the default.
%C
Displays the first two digits of the four-digit year as a decimal number (00-99). A year is divided by 100 and truncated to an integer.
%d
Displays the day of the month as a decimal number (01-31). In a two-digit field, a 0 is used as leading space fill.
%D
Displays the date in the format equivalent to %m/%d/%y.
%e
Displays the day of the month as a decimal number (1-31). In a two-digit field, a blank space is used as leading space fill.
%h
Displays the locale's abbreviated month name (a synonym for %b).
%H
Displays the hour (24-hour clock) as a decimal number (00-23).
%I
Displays the hour (12-hour clock) as a decimal number (01-12).
%j
Displays the day of year as a decimal number (001-366).
%k
Displays the 24-hour-clock hour clock as a right-justified, space-filled number ( 0 to 23).
%m
Displays the month of year as a decimal number (01-12).
%M
Displays the minutes as a decimal number (00-59).
%n
Inserts a <new-line> character.
%p
Displays the locale's equivalent of either AM or PM.
%r
Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale, this is equivalent to %I:%M:%S %p.
%S
Displays the seconds as a decimal number (00- 59).
%s
Displays the number of seconds since January 1, 1970, Coordinated Universal Time (CUT).
%t
Inserts a <tab> character.
%T
Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS .
%u
Displays the weekday as a decimal number from 1-7 (Sunday = 7). Refer to the %w field descriptor.
%U
Displays week of the year(Sunday as the first day of the week) as a decimal number[00 - 53] . All days in a new year preceding the first
Sunday are considered to be in week 0.
%V
Displays the week of the year as a decimal number from 01-53 (Monday is used as the first day of the week). If the week containing
January 1 has four or more days in the new year, then it is considered week 01; otherwise, it is week 53 of the previous year.
%w
Displays the weekday as a decimal number from 0-6 (Sunday = 0). Refer to the %u field descriptor.
%W
Displays the week number of the year as a decimal number (00-53) counting Monday as the first day of the week.
%x
Displays the locale's appropriate date representation.
%X
Displays the locale's appropriate time representation.
%y
Displays the last two numbers of the year (00-99).
%Y
Displays the four-digit year as a decimal number.
%Z
Displays the time-zone name, or no characters if no time zone is determinable.
%%
Displays a % (percent sign) character.
Modified Field Descriptors
The %E and %O field descriptors can be modified to indicate a different format or specification, as described in LC_TIME Category for the
Locale Definition Source File Format in AIX 5L Version 5.3 Files Reference. If the corresponding keyword (see the era, era_year, era_d_fmt,
and alt_digits keywords) is not specified or not supported for the current locale, the unmodified field descriptor value is used.
%Ec
Displays the locale's alternative appropriate date and time representation.
%EC
Displays the name of the base year (or other time period) in the locale's alternative representation.
%Ex
Displays the locale's alternative date representation.
%EX
Displays the locale's alternative time representation.
%Ey
Displays the offset from the %EC field descriptor (year only) in the locale's alternative representation.
%EY
Displays the full alternative year representation.
%Od
Displays the day of the month using the locale's alternative numeric symbols.
%Oe
Displays the day of the month using the locale's alternative numeric symbols.
%OH
Displays the hour (24-hour clock) using the locale's alternative numeric symbols.
%OI
Displays the hour (12-hour clock) using the locale's alternative numeric symbols.
%Om
Displays the month using the locale's alternative numeric symbols.
%OM
Displays minutes using the locale's alternative numeric symbols.
%OS
Displays seconds using the locale's alternative numeric symbols.
%Ou
Displays the weekday as a number in the locale's alternative representation (Monday=1).
%OU
Displays the week number of the year using the locale's alternative numeric symbols. Sunday is considered the first day of the week.
%OV
Displays the week number of the year using the locale's alternative numeric symbols. Monday is considered the first day of the week.
%Ow
Displays the weekday as a number in the locale's alternative representation (Sunday =0).
%OW
Displays the week number of the year using the locale's alternative numeric symbols. Monday is considered the first day of the week.
%Oy
Displays the year (offset from %C) in alternative representation.
Exit Status
This command returns the following exit values:
0
The date was written successfully.
>0
An error occurred.
Examples
1 To display current date and time, enter:
date
2 To set the date and time, enter:
date 0217142590
For a system using CST as its time zone, this sets the date and time to Sat Feb 17 14:25:00 CST 1990. Note: You must have root authority
to change the date and time.
3 To display the date and time in a specified format, enter:
date +"%r %a %d %h %y (Julian Date: %j)"
This displays the date shown in Example 2 as:
02:25:03 PM Fri 17 Feb 90 (Julian Date: 048)
Environment Variables
The following environment variables affect the execution of the date command.
LANG
Determines the locale to use when both LC_ALL and the corresponding environment variable (beginning with LC_) do not specify a locale.
LC_ALL
Determines the locale to be used to override any values for locale categories specified by the setting of LANG or any environment
variable beginning with LC_.
LC_CTYPE
Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single versus multibyte
character in an argument).
LC_MESSAGES
Determines the language in which messages should be written.
LC_TIME
Determines the contents of date and time strings written by date.
NLSPATH
Determines the location of message catalogues for the processing of LC_MESSAGES.
TZ
Specifies the time zone in which the time and date are written, unless the -u option is specified. If the TZ variable is not set and the
-u flag is not specified, an unspecified system default time zone is used.
Related Information
The environment file.
The localtime subroutine, strftime subroutine, time subroutine.
LC_TIME Category for the Locale Definition Source File Format in AIX 5L Version 5.3 Files Reference.
Understanding Locale in AIX 5L Version 5.3 National Language Support Guide and Reference discusses locale values.