Bash 时间格式 HH:MM 12 小时格式 AM/PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39357942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash time format HH:MM in 12 hour format AM/PM
提问by AtSim1
I am trying to print out the time in AM / PM format with this code:
我正在尝试使用以下代码以 AM / PM 格式打印时间:
#!/bin/bash
while [[ $# -gt 0 ]]
do
key=""
case $key in
--AMPM| --ampm)
while true;
do
#Time in AMPM format:
echo $(date +"%r")
sleep 1s;
clear;
done
esac
done
I get this: HH:MM:SS I want to get this: HH:MM
我得到这个:HH:MM:SS 我想得到这个:HH:MM
How can i alter the code to do so ? or why doesnt it work ?
我怎样才能改变代码呢?或者为什么它不起作用?
回答by Dawid Grabowski
Format date output:
格式化日期输出:
date +"%I:%M %p"
date +"%I:%M %P"
Where:
在哪里:
%I hour (01..12)
%M minute (00..59)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
回答by chepner
If you are using at least bash
4.2, you don't even need date
:
如果您至少使用bash
4.2,则甚至不需要date
:
printf '%(%I:%M %p)T\n'
回答by euphoria83
You can just replace the following line
您可以替换以下行
echo $(date +"%r")
with
和
echo $(date +"%r" | sed -e 's/\([0-9][0-9]:[0-9][0-9]\):[0-9][0-9]//')