Bash Date

To format Bash Date to a required one, bash shell provides date command along with many format options.

Bash Date Command

Following is the syntax of date command

 
  $ date
 

Format Bash Date with Options

As already said, you can format the Bash Date. And the format you wish may contain spaces as well.

Date command accepts options if provided any

 
  $ date +<format-option><format-option>
 

To format date with spaces, use the syntax

 
  $ date '+<format-option><format-option> <format-option>'
 

List of Bash Date Formatting Options

Following are the list of available options for date command :

Format option Part of Date Desciption Example Output
date +%a Weekday Name of weekday in short (like Sun, Mon, Tue, Wed, Thu, Fri, Sat) Mon
date +%A Weekday Name of weekday in full (like Sunday, Monday, Tuesday) Monday
date +%b Month Name of Month in short (like Jan, Feb, Mar ) Jan
date +%B Month Month name in full short (like January, February) January
date +%d Day Day of month (e.g., 01) 04
date +%D MM/DD/YY Current Date; shown in MM/DD/YY 02/18/18
date +%F YYYY-MM-DD Date; shown in YYYY-MM-DD 2018-01-19
date +%H Hour Hour in 24-hour clock format 18
date +%I Hour Hour in 12-hour clock format 10
date +%j Day Day of year (001..366) 152
date +%m Month Number of month (01..12) (01 is January) 05
date +%M Minutes Minutes (00..59) 52
date +%S Seconds Seconds (00..59) 18
date +%N Nanoseconds Nanoseconds (000000000..999999999) 300231695
date +%T HH:MM:SS Time as HH:MM:SS (Hours in 24 Format) 18:55:42
date +%u Day of Week Day of week (1..7); 1 is Monday 7
date +%U Week Displays week number of year, with Sunday as first day of week (00..53) 23
date +%Y Year Displays full year i.e. YYYY 2018
date +%Z  Timezone Time zone abbreviation (Ex: IST, GMT) IST

You may use any of the above-mentioned format options (first column) for the date command in the aforementioned syntax.

Examples

Bash Date Format MM-DD-YYYY

To format date in MM-DD-YYYY format, use the command  date +%m-%d-%Y .

#!/bin/bash
 
d=`date +%m-%d-%Y`
 
echo $d # 12-30-2017

Please observe the upper and lower case letters : %m for month, %d for day and %Y for year. %M would mean minutes.

Bash Date Format MM-YYYY

To format date in MM-YYYY format, use the command  date +%m-%Y .

#!/bin/bash
 
d=`date +%m-%Y`
 
echo $d # 12-2017

Bash Date Format: Weekday DD-Month, YYYY

To format date in MM-DD-YYYY format, use the command  date +%m-%d-%Y .

#!/bin/bash
 
d=`date '+%A %d-%B, %Y'`
 
echo $d #  Saturday 30-December, 2017

bash date format的更多相关文章

  1. javascript Date format(js日期格式化) (转)

    方法一:这个很不错,好像是 csdn 的 Meizz 写的: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) ...

  2. [转]javascript Date format(js日期格式化)

    方法一:这个很不错,好像是 csdn 的 Meizz 写的: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q)  ...

  3. javascript Date format(js日期格式化)

    这个用这比较爽,记录一下// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...

  4. [荐]javascript Date format(js日期格式化)

    cnblog:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html 方法一: // 对Date的扩展,将 Date  ...

  5. JS Date.Format

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  6. Javascript Date Format

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  7. Date Format, 时间戳格式化

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  8. js new Date().Format

    /** * * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符 * 年(y)可以用 1-4 ...

  9. GG同步sqlserver报错一个案例 Invalid date format

    在里面Oracle表同步sqlserver时间,在sqlserver当应用程序数据的结束.您可能会遇到这个错误. 2014-05-17 17:20:24 WARNING OGG-01154 SQL e ...

随机推荐

  1. Sass函数-join()函数

    join() 函数是将两个列表连接合并成一个列表. >> join(10px 20px, 30px 40px) (10px 20px 30px 40px) >> join((b ...

  2. lambda表达式以及stream流式api用法

    https://www.cnblogs.com/aoeiuv/p/5911692.html 这篇文章讲的简单全面,记录下 kotlin一些符号的用法 https://www.cnblogs.com/l ...

  3. 谈一谈测试驱动开发(TDD)的好处以及你的理解

    DD是指在编写真正的功能实现代码之前先写测试代码,然后根据需要重构实现代码.在JUnit的作者Kent Beck的大作<测试驱动开发:实战与模式解析>(Test-Driven Develo ...

  4. elasticsearch添加访问密码

    1.将x-pack复制到elasticsearch的plugins目录下面 2.启动elasticsearch .bin/elasticsearch & 3.修改指定用户密码 PUT http ...

  5. jquery 小知识

    $("p:eq(0)") :表p标签的第一个元素 $("p:eq(1)") :表p标签的第二个元素

  6. HTML5 canvas绘制文本

    demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  7. centos 6.5 切换用户

    root --> user,无需密码 su username user --> root,需要 root 密码 su root root 可省略

  8. 【HDOJ6611】K Subsequence(费用流)

    题意:给定一个长为n的正整数序列,要求从中取出至多k个不下降序列,使得它们的和最大,求这个和 n<=2e3,k<=10,a[i]<=1e5 思路:极其考验模板,反正我的spfa和zk ...

  9. LOJ 2997 「THUSCH 2017」巧克力——思路+随机化+斯坦纳树

    题目:https://loj.ac/problem/2977 想到斯坦纳树.但以为只能做 “包含一些点” 而不是 “包含一些颜色” .而且不太会处理中位数. 其实 “包含一些颜色” 用斯坦纳树做也和普 ...

  10. SQL各种JOIN

    JOIN(= INNER JOIN):返回匹配的结果,没有匹配则没结果: LEFT JOIN(= LEFT OUTER JOIN):返回匹配的与左表的所有数据: RIGHT JOIN(= RIGHT ...