[英]Format string to add leading zero in 24 hour time
我正在使用PowerShell的-match
运算符和正则表达式\\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\\d{2})
来获取日期和时间a重新启动应该发生。
样本数据:
Patching - Prod - Fri 2:00
Patching - Prod - Fri 22:00
Patching - Prod - Thu 22:00
Patching - Prod - Fri 22:00
Patching - Prod - Sat 18:00
Patching - Prod - Sun 2:00
Patching - Prod - Sun 00:00
Patching - Prod - Sat 2:00
$Rebootinfo = "Patching - Prod - Sat 2:00"
"$Rebootinfo" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null
这很好用,但是我发现当时间是2:00 AM
我会获得2:00
并且我希望将02:00
的前导零填充到该结果中,如果时间是午夜,则结果将是0
而不是所需的00:00
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null
$a = $Matches[2]
$a.ToString("00:00")
返回错误找不到“ ToString”的重载和参数计数:“ 1”。
我这样做的目的是将数据传递到PowerShell中,以获取直到该重启时间为止的天数。 例如,如果在周六运行,则周日2 AM需要增加1天。
您不能在字符串上使用数字格式,因此您需要先将小时/分钟转换为int。 以下是一些示例:
#Convert 2:00 to 200 int-number and format it to 00:00-style -> 02:00.
#18:00 -> 1800 -> 18:00
"{0:00:00}" -f ([int]$a.Replace(":",""))
要么
#Capture hour and minutes in their own groups
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9])[:](\d{2})" | Out-Null
#Format 00 only works with digits, so convert to int
"{0:00}:{1:00}" -f [int]$Matches[2], [int]$Matches[3]
或者,您可以将其解析为DateTime并转换回具有正确格式的字符串(如果需要,也可以使用DateTime对象)。
$date = [datetime]::ParseExact($Matches[0], "ddd H:mm", [cultureinfo]::InvariantCulture)
$date.ToString("ddd HH:mm", [cultureinfo]::InvariantCulture)
您可以这样做:
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null $a = $Matches[2] $ts = [TimeSpan]::Parse($a) $formatted = $ts.ToString("c").Substring(0, 5) $formatted
它为$formatted
输出02:00
。
您的帖子似乎没有问题(目标,但没有问题)。 因此,我猜您的问题是“为什么正则表达式不返回'02:00AM'”?
由于源字符串在2之前不包含零,因此您将无法获得源字符串中不包含零的匹配项。 您需要将其添加为单独的步骤。
通过使用.NET的内置日期时间解析,可以避免一些麻烦: [datetime]::parseexact
。 不幸的是,如果星期日不是当天,ParseExact无法处理诸如“ Sun 2:00 AM”之类的字符串,因此需要一些额外的工作。 这是一些相同的示例代码。
$Rebootinfo = "Patching - Prod - Thu 2:00AM"
$splitUp = $Rebootinfo -split "\b(Thu|Fri|Sat|Sun)"
# $splitUp[-1] now contains time and $splitUp[-2] contains day of week
$cult = [Globalization.CultureInfo]::InvariantCulture
try {
$rebootTime = [datetime]::parseexact( $splitUp[-1], " h:mmtt", $cult)
} catch {
# put your own error handling here
throw "Date time parse failed"
}
$weekDayToWeekDayNumber = @{Sun=0;Mon=1;Tue=2;Wed=3;Thu=4;Fri=5;Sat=6}
$rebootWeekDayNumber = $weekDayToWeekDayNumber[$splitUp[-2]]
$todayWeekDayNumber = $weekDayToWeekDayNumber[[datetime]::today.DayOfWeek.tostring().substring(0,3)]
# This calculation fails if the reboot day of the week is same as current
# day of week and reboot time is before current time. However I'm guessing
# this won't be a problem because if this
# happens you've already missed the boot or the boot is almost a week away.
# Assuming the later: since you only have days of the week (and not dates)
# I'm guessing that
# boots almost a week away aren't a concern. The reason is that if you
# handle boots almost a
# week away, there's no guarantee (that I see) that the boot won't be a
# little more than a week away (since you don't know exactly when the boot
# is, hence the script). And if boots can be more than a week away you won't
# be able to distinguish between boots this week and boots next week (since
# you only have the day of the week).
# However if this is a problem, just compare $rebootTime to [datetime]::now
# and if less, then add 7 more days to $rebootTime.
$rebootTime = $rebootTime.AddDays( ($rebootWeekDayNumber - $todayWeekDayNumber + 7) % 7)
write-host Amount of time till reboot ($rebootTime - [datetime]::now)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.