首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从shell脚本中的日期字符串fomat (2021-09-04T20:02:33,315Z)中提取日期、小时、分钟和秒

从shell脚本中的日期字符串fomat (2021-09-04T20:02:33,315Z)中提取日期、小时、分钟和秒,可以使用字符串处理工具和正则表达式来实现。

以下是一个示例的Shell脚本代码,用于提取日期、小时、分钟和秒:

代码语言:txt
复制
#!/bin/bash

# 定义日期字符串
date_string="2021-09-04T20:02:33,315Z"

# 提取日期部分
date=$(echo $date_string | cut -d 'T' -f 1)
echo "日期: $date"

# 提取时间部分
time=$(echo $date_string | cut -d 'T' -f 2 | cut -d ',' -f 1)
echo "时间: $time"

# 提取小时、分钟和秒
hour=$(echo $time | cut -d ':' -f 1)
minute=$(echo $time | cut -d ':' -f 2)
second=$(echo $time | cut -d ':' -f 3)
echo "小时: $hour"
echo "分钟: $minute"
echo "秒: $second"

运行以上脚本,将会输出以下结果:

代码语言:txt
复制
日期: 2021-09-04
时间: 20:02:33
小时: 20
分钟: 02
秒: 33

这段脚本首先使用cut命令根据字符T将日期字符串分割成日期和时间两部分,然后再次使用cut命令根据字符,将时间部分分割成小时、分钟和秒。最后,将提取到的日期、小时、分钟和秒分别赋值给对应的变量,并输出结果。

请注意,这只是一个示例代码,实际应用中可能需要根据具体的需求进行适当的修改和优化。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Python 学习入门(10)—— 时间

Python格式化日期时间的函数为datetime.datetime.strftime();由字符串转为日期型的函数为:datetime.datetime.strptime(),两个函数都涉及日期时间的格式化字符串,列举如下: %a     Abbreviated weekday name %A     Full weekday name %b     Abbreviated month name %B     Full month name %c     Date and time representation appropriate for locale %d     Day of month as decimal number (01 - 31) %H     Hour in 24-hour format (00 - 23) %I     Hour in 12-hour format (01 - 12) %j     Day of year as decimal number (001 - 366) %m     Month as decimal number (01 - 12) %M     Minute as decimal number (00 - 59) %p     Current locale's A.M./P.M. indicator for 12-hour clock %S     Second as decimal number (00 - 59) %U     Week of year as decimal number, with Sunday as first day of week (00 - 51) %w     Weekday as decimal number (0 - 6; Sunday is 0) %W     Week of year as decimal number, with Monday as first day of week (00 - 51) %x     Date representation for current locale %X     Time representation for current locale %y     Year without century, as decimal number (00 - 99) %Y     Year with century, as decimal number %z, %Z     Time-zone name or abbreviation; no characters if time zone is unknown %%     Percent sign

03
领券