powershell - Powershell 日期时间过滤器未使用正确的 ShortDate 模式
问题描述
我正在尝试在 Get-ChildItem 函数上过滤 LastWriteTime,但是当我尝试使用英国日期进行过滤时,它会失败,因为它不是美国格式。
当我运行时,(Get-Culture).DateTimeFormat.ShortDatePattern
我收到dd/MM/yyy
正确的格式。
但是,当我执行 powershell 过滤器语句时:-
GCI 'C:\ | ?{$_.LastWriteTime -ge '21/01/2018'}
我收到以下信息:-
Cannot convert value "21/01/2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
但是,如果我将日期更改为01/21/2018
我收到正确的结果。
我尝试使用$_.LastWriteTime.ToString('dd-MM-yyyy') -ge '01[![enter image description here][1]][1]/01/2018'
无法正常工作的,因为它从 2017 年返回日期。
解决方案
首先将您的日期字符串转换为日期对象:
$date = get-date '21/01/2018'
gci C:\ | ? {$_.LastWriteTime -ge $date }
推荐阅读
- r - R:将大型数据帧转换为成对相关矩阵
- python - 如何让我的游戏中的零食出现随机颜色?
- java - JPA WrongArgumentException:SQL 字符串不能为 NULL:尽管 CrudRepository 保存函数接收填充的实体
- r - 绘制 geom_line() + geom_point() R
- javascript - 为什么在我的 jQuery 循环中返回不起作用?
- amazon-web-services - 使用 AWS Java SDK 获取 DynamoDB 中的列值计数
- knockout.js - Uncaught (in promise) ReferenceError: ValidationBase is not defined
- android - 运行 Ionic cordova 时出现无限循环 run android --livereload
- node.js - express 接收 favicon.ico 作为默认路由的参数
- sql - 有没有一种方法可以按 id 升序保存我的表格