[玩转系统] PowerShell 将字符串转换为日期 | PowerShell 的 5 个最佳示例
作者:精品下载站 日期:2024-12-14 04:49:35 浏览:15 分类:玩电脑
PowerShell 将字符串转换为日期 | PowerShell 的 5 个最佳示例
PowerShell 将字符串转换为日期简介
在许多情况下,可能需要将字符串转换为日期变量或日期时间对象。在 PowerShell 中,可以通过多种方式完成此操作。可以通过使用类型转换、获取美国格式的日期、使用日期时间解析来完成此操作。许多目的都需要这种转换,例如将字符串对象发送到数据库表,因此当以字符串格式接收来自用户的输入,然后需要格式化以进行进一步处理时,需要进行转换。本文将详细介绍将字符串对象转换为日期时间对象的各种方法。
语法:
以下是日期时间解析的语法:
[Datetime]::ParseExact('07/15/2019', 'MM/dd/yyyy', $null)
其中第一个参数是需要转换的字符串,第二个参数是要生成的日期的格式,第三个参数是空字符。
代码:
Write-Host "Welcome to string to date conversion example"
$string='11/11/2019'
Write-Host "The variable type now is" $string.GetType()
$string=[Datetime]::ParseExact($string, 'MM/dd/yyyy', $null)
Write-Host "After conversion the type is" $string.GetType() "and the value is" $string
输出:
PowerShell 中可用的日期格式类型
在了解将字符串转换为数据对象的各种方法之前,了解 PowerShell 中可用的各种日期类型非常重要。在转换过程中这会很有帮助。
以下是 PowerShell 中可用的各种日期格式。
d: 表示 ShortDate
D: 表示 LongDate
f: 表示长日期,短时间
F:表示长日期、长时间
t:表示短时间格式
T: 表示长时间格式
m, M: 月日格式
g: 表示一般数据时间短格式
G:表示一般数据时间长格式
Y 或 y: 表示年月模式
PowerShell 将字符串转换为日期的示例
以下是 PowerShell 的示例:
例子#1
代码:
Write-Host "Date in long date format" -ForegroundColor DarkYellow
Get-Date -Format D
Write-Host "long date short time" -ForegroundColor DarkYellow
Get-Date -Format f
Write-Host "long date long time" -ForegroundColor DarkYellow
Get-Date -Format F
Write-Host "Date in general date time short format" -ForegroundColor DarkYellow
Get-Date -Format g
Write-Host "Date in general date time long format" -ForegroundColor DarkYellow
Get-Date -Format G
Write-Host "month day format" -ForegroundColor DarkYellow
Get-Date -Format m
Write-Host "Date in year format" -ForegroundColor DarkYellow
Get-Date -Format y
输出:
例子#2
代码:
Write-Host "String to Date conversion examples"
Write-Host "example of yymmddhhmm format"
[System.DateTime]::ParseExact('1605221412','yyMMddHHmm',$null)
Write-Host "Global time example"
[System.DateTime]::ParseExact('1805221412','yyMMddHHmm',[System.Globalization.DateTimeFormatInfo]::CurrentInfo)
Write-Host "US Timings example"
[System.DateTime]::ParseExact('1804221610','yyMMddHHmm',[System.Globalization.CultureInfo]::GetCultureInfo('en-US'))
Write-Host "Complex example of custome pattern"
[string]$test = 'year 2020 and date 12 and month feb and time 9 00'
[string]$testpattern = '\year yyyy an\d \da\te dd an\d \mon\t\h MMM an\d \ti\me H mm'
[DateTime]::ParseExact($test, $testpattern, $null)
Write-Host "mmddyy format example"
[System.DateTime]::ParseExact('111199','mmddyy',$null)
Write-Host "mmddyyyy format example"
[System.DateTime]::ParseExact('11111999','mmddyyyy',$null)
输出:
例子#3
代码:
Write-Host "Demo about coverting strings into dates and finding difference" -ForegroundColor DarkYellow
$stringa="01/01/2020"
$stringb="01/01/1990"
Write-Host "The current data type of the objects is" $stringa.GetType() "and" $stringb.GetType() -ForegroundColor DarkYellow
Write-Host "Converting the first string to date" -ForegroundColor DarkYellow
$date1=[System.DateTime]::ParseExact($stringa,'mm/dd/yyyy',$null)
Write-Host "the value of a the first date after conversion is "$date1 "and the type is" $date1.GetType() -ForegroundColor DarkYellow
Write-Host "Converting the second string to date" -ForegroundColor DarkYellow
$date2=[System.DateTime]::ParseExact($stringb,'mm/dd/yyyy',$null)
Write-Host "the value of a the first date after conversion is "$date2"and the type is" $date2.GetType() -ForegroundColor DarkYellow
$difference= $date2- $dateB
Write-Host "number of days difference between the two dates is " $difference.Days -ForegroundColor DarkYellow
Write-Host "number of hours difference between the two dates is " $difference.Hours -ForegroundColor DarkYellow
Write-Host "number of minutes difference between the two dates is " $difference.Minutes -ForegroundColor DarkYellow
Write-Host "number of seconds difference between the two dates is " $difference.Seconds -ForegroundColor DarkYellow
Write-Host "number of milliseconds difference between the two dates is " $difference.Milliseconds -ForegroundColor DarkYellow
Write-Host "number of total days difference between the two dates is " $difference.TotalDays -ForegroundColor DarkYellow
Write-Host "number of total hours difference between the two dates is " $difference.TotalHours -ForegroundColor DarkYellow
Write-Host "number of total minutes difference between the two dates is " $difference.TotalMinutes -ForegroundColor DarkYellow
Write-Host "number of total seconds difference between the two dates is " $difference.TotalSeconds -ForegroundColor DarkYellow
Write-Host "number of total milliseconds difference between the two dates is " $difference.TotalMilliseconds -ForegroundColor DarkYellow
输出:
使用日期时需要考虑的重要事项
在将字符串类型转换为日期之前,了解系统的本地设置非常重要。如果系统遵循英国日期格式,而我们指定美国时间格式,则可能会生成错误或显示错误的日期。例如,7/16/2020 在美国格式中表示 7 月 16 日,而在英国,它会产生错误,因为第二个被视为月份,而月份不能大于 12。为了克服这个问题,可以使用当地文化设置。
例子#4
代码:
Write-Host "Local settings demo"
$string="13/2/2020"
$date= [DateTime] $string
输出:
在上面的输出中,系统会抛出错误,因为美国时区和月份在先,因此它不能大于 12。如果我们将 13 更改为 12,则会产生以下输出。
代码:
Write-Host "Local settings demo"
$string="12/2/2020"
$date= [DateTime] $string
Write-Host $date -ForegroundColor DarkYellow
输出:
例子#5
代码:
$tdate="06/16/2016 3:14:03 PM"
$CTDC = (Get-Culture).DateTimeFormat
Write-Host "Systems culture data time format is" $CTDC -ForegroundColor DarkYellow
$ctdcDateFormat = $CTDC.ShortDatePattern
Write-Host "systems short date pattern is" $ctdcDateFormat -ForegroundColor DarkYellow
$ctdcTimeFormat = $CTDC.LongTimePattern
Write-Host "systems long time pattern is" $ctdcTimeFormat -ForegroundColor DarkYellow
$DateTimeFormat = "$ctdcDateFormat $ctdcTimeFormat"
$DateTime = [DateTime]::ParseExact($tdate,$DateTimeFormat,[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)
输出:
结论
因此,本文详细介绍了从字符串对象到日期时间对象的转换。它详细解释了将字符串转换为日期时间对象的各种方法,以及如何将字符串对象转换为特定的日期时间格式。它还详细解释了转换过程中可能遇到的错误以及如何克服它们。在转换之前了解当前系统的格式也是明智的。要了解更多详细信息,建议编写示例程序并进行练习。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag