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.

转载于:https://blog.51cto.com/baser/1407458

shell date相关推荐

  1. shell实例第20讲:linux shell date的用法

    linux shell date的用法 转自:https://www.cnblogs.com/faberbeta/p/linux-shell002.html 1.date中的参数 %% 一个文字的 % ...

  2. Shell Date命令

    shell Date命令 1.Date命令 date +%Y 以四位数字格式打印年份 date +%y 以二位数字格式打印年份 date +%m 月份 date +%d 日期 date +%H 小时 ...

  3. 8月第四周 | shell date、TGI衡量用户偏好

    说好周更,总不能第一周就放鸽子,整理好了思路,开始下笔. 每周的这种杂记会比较零散.混乱而不成体系,我只是想记录下 我这周做了什么.遇到了什么不懂的.我怎么解决的 或者 我学了什么新知识.有什么新体悟 ...

  4. linux shell date 日期格式转换

    在Shell环境里,我们获取时间的命令是date,但date出来的时间日期格式.内容可能不是我们所想要的,所以我们可能需要对它进行一些格式化输出. 常用的时间域如下: % Y 年(例如:1970,20 ...

  5. linux date 计算时间差,linux shell date 时间运算以及时间差计算方法

    最近一段时间,在处理Shell 脚本时候,遇到时间的处理问题. 时间的加减,以及时间差的计算. 获取当前时间戳date +%s 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加 ...

  6. shell date 命令

    BASH SHELL中可以定义变量显示当前日期 显示当前日期是 DATE=`date +%Y%m%d`  +号后面是定义格式为年月日 显示前一天为 DATE1=`date -d '1 days ago ...

  7. shell date 扩展功能(-d参数)

    扩展功能  date 工具可以完成更多的工作,不仅仅只是打印出当前的系统日期.您可以使用它来得到给定的日期究竟是星期几,并得到相对于当前日期的相对日期.了解某一天是星期几  GNU 对 date 命令 ...

  8. shell date 获取昨天日期

    使用date -d 选项:  date  +"%Y%m%d" -d  "+n days"         今天的后n天日期 date  +"%Y%m% ...

  9. shell date 格式化

    https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/ DATE=`date '+%d ...

最新文章

  1. 资本主义社会是不存在人道的
  2. 从零开始入门 | Kubernetes 中的服务发现与负载均衡
  3. DS18B20 驱动编写
  4. 工作总结1:代码中漫花谷出现很多NBSP
  5. Ken Block 漂移大叔,程序实现精准漂移算法。
  6. [国嵌笔记][025][ARM指令分类学习]
  7. 以二进制的形式保存在数据库中
  8. python柱状图代码_python+matplotlib实现礼盒柱状图实例代码
  9. golang基础之三-字符串,时间,流程控制,函数
  10. 系统出错, 发生系统错误 1067, 进程意外终止. 解决方法
  11. 百度网盘链接在线解析网站_利用在线服务下载百度网盘的文件
  12. CDR中神奇的卷页滤镜
  13. SpringBoot-数据库连接池(java配置和Yaml配置)
  14. python的分支语句if基本用法
  15. Bonree ONE荣获信通院“2022IT新治理年度明星产品”
  16. Cookie&Session
  17. wps宏编辑器-js宏录制运行
  18. 如何快速制作脚本?间隔时间自动使用按键精灵游戏技能辅助脚本
  19. 计算机毕业设计Java健身俱乐部管理系统(源码+系统+mysql数据库+lw文档)
  20. MySQL——常用命令

热门文章

  1. python t检验_讲讲统计学中T检验的种类
  2. 【Linux基础 01】Linux 下的文件系统
  3. LeetCode 编程 二
  4. linux安全配置-将ssh服务隐藏于Internet(端口碰撞)
  5. 页面生命周期:DOMContentLoaded, load, beforeunload, unload
  6. 如何写出《黄焖鸡米饭是怎么火起来的》这样的文章
  7. BlueMix与商业智能BI(第二部分:Bluemix应用创建)
  8. MySQL杂记(更新时间——2014.05.23)
  9. vim中的 recording
  10. 如果我要...(开发版)