1. 程式人生 > >Linux Shell 日期格式

Linux Shell 日期格式

可以使用幫助命令(date --help)檢視說明:

[[email protected] Shell]$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
-f, --file=DATEFILE like --date once for each line of DATEFILE -I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format. TIMESPEC='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and
time to the indicated precision. -r, --reference=FILE display the last modification time of FILE -R, --rfc-2822 output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600 --rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC='date', 'seconds', or 'ns' for date and time to the indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00 -s, --set=STRING set time described by STRING -u, --utc, --universal print or set Coordinated Universal Time (UTC) --help display this help and exit --version output version information and exit FORMAT controls the output. Interpreted sequences are: %% 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., 20) %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, space padded ( 0..23); same as %_H %l hour, space padded ( 1..12); same as %_I %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 time zone (e.g., -0400) %: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) By default, date pads numeric fields with zeroes. The following optional flags may follow '%': - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possible After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available. Examples: Convert seconds since the epoch (1970-01-01 UTC) to a date $ date --date='@2147483647' Show the time on the west coast of the US (use tzselect(1) to find TZ) $ TZ='America/Los_Angeles' date Show the local time for 9AM next Friday on the west coast of the US $ date --date='TZ="America/Los_Angeles" 09:00 next Fri' GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'date invocation'

在使用shell指令碼時,經常會需要用到日期和時間,然後格式非常多,日期和時間格式用的比較多的有如下:

2016-01-01           //年-月-日
2016-01-01 19:46:02  //年-月-日 時:分:秒
2016/01/01 19:46:02  //年/月/日 時:分:秒

在終端下執行以下命令:

[yang@master Shell]$ date +%F
2016-12-15
[yang@master Shell]$ date +%Y-%m-%d
2016-12-15
[yang@master Shell]$ date +%Y%m%d
20161215
[yang@master Shell]$ date +%F' '%H:%M:%S
2016-12-15 19:49:57
[yang@master Shell]$ date +%Y-%m-%d' '%H:%M:%S
2016-12-15 19:50:22
[yang@master Shell]$ date +%Y/%m/%d' '%H:%M:%S
2016/12/15 19:54:22

以下指令碼也可能輸出上述日期或時間格式:

#!/usr/bin/env bash

echo "類似日期格式為:2016-01-01,今天是" $(date +%F)
echo "類似日期格式為:2016-01-01,今天是" $(date +%Y-%m-%d)
echo "類似日期格式為:20160101,今天是" $(date +%Y%m%d)

echo "類似時間格式為:2016-01-01 08:08:08,現在是" $(date +%F' '%H:%M:%S)
echo "類似時間格式為:2016-01-01 08:08:08,現在是" $(date +%Y-%m-%d' '%H:%M:%S)
echo "類似時間格式為:2016-01-01 08:08:08,現在是" $(date +%Y/%m/%d' '%H:%M:%S)

指令碼執行後輸出如下:

[yang@master Shell]$ vi date_format.sh 
[yang@master Shell]$ ./date_format.sh 
類似日期格式為:2016-01-01,今天是 2016-12-15
類似日期格式為:2016-01-01,今天是 2016-12-15
類似日期格式為:20160101,今天是 20161215
類似時間格式為:2016-01-01 08:08:08,現在是 2016-12-15 17:20:33
類似時間格式為:2016-01-01 08:08:08,現在是 2016-12-15 17:20:33
類似時間格式為:2016-01-01 08:08:08,現在是 2016/12/15 17:20:33

一些格式說明:

格式 說明 示例
%F 輸出完整日期,與%Y-%m-%d效果一樣 2016-12-15
%Y 年份
%m 月份 01,02,…12
%d 01,02,…,31
%H 小時 00,01,…23
%M 分鐘 00,01,…59
%S 00,01,…59

相關推薦

Linux Shell 日期格式

可以使用幫助命令(date --help)檢視說明: [[email protected] Shell]$ date --help Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|-

shell日期格式和數字運算

cond ech XP span %d expr 日期格 pan 小時 #!/bin/bash #declare d="2016-05-06 00:00:00" #d1=$(date -d "$d" +%s); #echo $d1 #d2=$(date +%Y%m%d%

Linux date日期格式及加減運算

                顯示時間是個常用的命令,在寫shell指令碼中也經常會用到與日期相關檔名或時間顯示。無論是linux還是windows下都是date命令。Linux下date命令用法date [OPTION]… [+FORMAT]date [-u|--utc|--universal] [MMD

LinuxShell日期格式

浪費了“黃金五年”的Java程式設計師,還有救嗎? >>>   

shell指令碼 連線資料庫 新增資料 查詢資料 連線不同資料庫 shell日期獲取 格式三分鐘前 上週 昨天修改

註釋:shell 單括號運算子號:a=$(date);等同於:a=`date` 雙括號運算子:a=$((1+2));echo $a;等同於:a=`expr 1 + 2` 東西有點多,就一起都講了。   日期格式: 獲取當前日期:1,date "+%Y-%m-%d %

Linux Shell基礎 Shell指令碼格式及執行方式

概述 在 Linux 的指令碼中,只要是基於 Bash語法寫的Shell指令碼第一行必須是"#!/bin/bash",用來宣告此檔案是一個指令碼。 執行方式 Shell 指令碼的執行主要有以下兩種方法: 賦予執行許可權,直接執行 這種方法是最常用的 Shell 指令碼執行方法,也最為直接簡單。就是賦

0083-【Linux-Shell】-date命令格式

獲得當天的日期 date +%Y-%m-%d 輸出: 2011-07-28 date1=$(date --date='1 days ago +%Y%m%d') #前一天的日期 date1=$(date --date='2 days ago +%Y%m%d')

Linux--Shell指令碼逐行讀文字處理並curl傳送post請求json格式等問題

問題:Shell指令碼逐行讀取文字,字串分割,Curl傳送Post請求Json格式等相關問題 在linux中使用curl傳送一個post請求時,帶有json的資料,在傳送時發現json中的變數沒有解析出來: curl -d '{"Pin": "${line}"}' -H"

Linux shell 獲取當前日期/時間

當前日期 [[email protected] DFJR-ERP]# cur_date=”date +%Y-%m-%d” [[email protected] DFJR-E

linux shell中curl 傳送post請求json格式問題 【轉】

今天在linux中使用curl傳送一個post請求時,帶有json的資料,在傳送時發現json中的變數沒有解析出來 如下 curl -i -X POST -H 'Content-type':'application/json' -d {"BTime":""$bt

Linux Shell——bash shell 腳本簡介

運行環境 linux 關鍵字 程序 單詞 bash shell 腳本簡介shell 運行環境如果你運行的是 Unix 或 Linux 系統,例如 Ubuntu,Red Hat,SUSE Linux,還有macOS,都是內置了 bash shell 的,所以你不需要額外配置所謂的開發

oracle 日期格式操作

簡寫 加減 月份 oracle ans 更新時間 ica 語言 日期格式 原文轉自:http://hi.baidu.com/gzfvb/blog/item/5062b7f008eb70a8a50f5206.html Oracle日期格式: to_date("

寫了一個簡單的Linux Shell用來下載文件

logs -- spi http col 內容 style bre shell #!/bin/sh for (( i=0; i<30; i=i+1 )); do # 利用spider來探測請求的資源是否存在,並把請求的結果寫入到一個文件 wget -

Linux Shell 腳本的小技巧——不定期更新

小技巧 linux shell 本文旨在積累平時寫 shell 腳本是遇到的小問題的解決方法。在這裏不定期的更新,以便以後查閱、沿用。 一,在shell 腳本中定義默認值 1. 對默認變量直接賦值 read -p "input>>:" nub nub=

殺死Linux Shell進程

shel china 命名 -name 登錄shell 方法 gin p s 進程 1、查找目標shell進程: 比如:我想要查找 odm_clean 腳本執行進程 ps -ef | grep "odm_clean" hadoop 3702 46809

針對兩種不同的日期格式進行轉成年月日時分秒的格式

provide mes nbsp code oba cnblogs -m bsp class 有兩個時間字符串,格式分別是 string dateEnglish = "1-6-14 8:25";//格式為 月-日-年 string dateFranch = "13-1-1

轉換日期格式的工具類

ring mage code edate print util println pan date() 寫一個工具類,用來轉換固定的日期格式: import java.text.SimpleDateFormat; import java.util.Date; public

linux shell自定義函數(定義、返回值、變量作用域)介紹

shel 自己 lai cell define ber article clas ner linux shell 可以用戶定義函數,然後在shell腳本中可以隨便調用。下面說說它的定義方法,以及調用需要註意那些事項。 一、定義shell函數(define function)

Linux shell 命令行下查詢外網IP

shell命令行查ip查詢IP在網頁上打開網址就可以顯示,但是在命令行下可以安裝w3m/Links/Lynx這些命令行瀏覽器,但是為了這個又感覺不方便,所以很多查IP網站提供了UNIX/LINUX的。命令行查詢(詳細): UNIX/Linux: #curl cip.cc Windows: >tel

linq中日期格式轉換或者比較,程序報錯說不支持方法的解決辦法

格式 var lec fun edi nbsp diff sql 方法 public void TestMethod1(){using (var _context = new hotelEntities()){var rq = DateTime.Now.Date;var q