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. [AtCoder] Yahoo Programming Contest 2019

    [AtCoder] Yahoo Programming Contest 2019   很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...

  2. Linux中检查内存使用情况的命令

    Linux操作系统包含大量工具,所有这些工具都可以帮助您管理系统.从简单的文件和目录工具到非常复杂的安全命令,在Linux上没有太多不能做的事情.而且,虽然普通桌面用户可能不需要在命令行熟悉这些工具, ...

  3. Java中避免空指针常见的方法

    什么是空指针? 当一个变量的值为null时,在Java里表示一个不存在的空对象,没有实际内容,没有给它分配内存,null也是对象成员的默认值.所以,一个对象如果没有进行初始化操作,这时如果调用这个对象 ...

  4. join()、split()

    join()用于把数组转化为字符串 var arr=['hello','world','kugou']; document.write(arr.join(''));//helloworldkugou ...

  5. Session过期,如何跳出iframe框架页的问题

    跳出框架页,实际上是更改父页面地址.那么更改父页面地址很简单即: window.parent.location='/Login/loginindex'; 这里说session过期,那么浏览器端的任何请 ...

  6. 阿里云HPC助力新制造 | 上汽仿真计算云SSCC

    随着上汽集团与阿里云的合作开展,阿里云各项技术逐步深入到上汽汽车研发领域的核心业务实现落地.其中上海汽车集团股份有限公司乘用车分公司(以下简称上汽乘用车)与阿里云共建的仿真计算混合云就是新制造产业升级 ...

  7. php max()函数 语法

    php max()函数 语法 作用:从所有参数中找到最大数 语法:max(X,Y,Z) 或者max(array(X,Y,Z)) 参数:max函数中参数至少一个,可以多个参数,也可以是数组. 说明:如果 ...

  8. k-近邻算法(kNN)测试算法:作为完整程序验证分类器

    #测试算法:作为完整程序验证分类器 def datingClassTest(): hoRatio = 0.10 #设置测试集比重,前10%作为测试集,后90%作为训练集 datingDataMat,d ...

  9. HDU 4812 (点分治)

    题目:https://vjudge.net/contest/307753#problem/E 题意:给你一颗树,树上每个点都有个权值,现在问你是否存在 一条路径的乘积 mod 1e6+3  等于 k的 ...

  10. 【靶场训练_DVWA】Command Execution

    low 利用: ;ls ../../ 源码分析: <?php if( isset( $_POST[ 'submit' ] ) ) { //将ip对应的值复制给target $target = $ ...