时间获取

本文详细介绍了date命令中常用的参数及其用途,包括日期时间的各种格式化选项,如年、月、日、小时、分钟等,以及如何获取特定日期的时间表示形式。通过实际应用场景示范了如何使用这些参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用date参数 备查
上下午
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case


月、星期
%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)
%W week number of year, with Monday as first day of week (00..53)
%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)


年月日
%Y year
%y last two digits of year (00..99)
%C century; like %Y, except omit last two digits (e.g., 21)
%m month (01..12)
%w day of week (0..6); 0 is Sunday
%u day of week (1..7); 1 is Monday
%d day of month (e.g, 01)
%e day of month, space padded; same as %_d
%j day of year (001..366)


时分秒
%H hour (00..23)
%I hour (01..12)
%k hour ( 0..23)
%l hour ( 1..12)
%M minute (00..59)
%N nanoseconds (000000000..999999999)
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)


时区
%z +hhmm numeric timezone (e.g., -0400)
%:z +hh:mm numeric timezone (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)


综合
%D date; same as %m/%d/%y
%F full date; same as %Y-%m-%d
%x locale's date representation (e.g., 12/31/99)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%T time; same as %H:%M:%S
%X locale's time representation (e.g., 23:13:48)


实例场景
月末的日期
date -d "2011-3-1 -1 days" +%Y%m%d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值