PowerShell แปลงสตริงเป็นวันที่ | 5 ตัวอย่าง PowerShell ยอดนิยม
ข้อมูลเบื้องต้นเกี่ยวกับ PowerShell แปลงสตริงเป็นวันที่
ในหลายกรณี อาจจำเป็นต้องแปลงสตริงเป็นตัวแปรวันที่หรือวัตถุวันที่-เวลา ใน PowerShell สามารถทำได้หลายวิธี วิธีการบางอย่างที่สามารถทำได้โดยใช้การเลือกประเภท รับวันที่ในรูปแบบสหรัฐอเมริกา โดยใช้การแยกวิเคราะห์วันที่และเวลา การแปลงนี้จำเป็นสำหรับวัตถุประสงค์หลายประการ เช่น การส่งออบเจ็กต์สตริงไปยังตาราง DB ดังนั้นจึงจำเป็นต้องมีการแปลงเมื่อได้รับอินพุตจากผู้ใช้ในรูปแบบสตริง จากนั้นจำเป็นต้องจัดรูปแบบเพื่อดำเนินการต่อไป บทความนี้จะกล่าวถึงรายละเอียดเกี่ยวกับวิธีการต่างๆ ที่สามารถแปลงวัตถุสตริงเป็นวัตถุวันที่-เวลาได้
ไวยากรณ์:
ด้านล่างนี้เป็นไวยากรณ์ของ Datetime Parse:
[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เอาต์พุต:
สิ่งสำคัญที่ต้องพิจารณาขณะทำงานกับวันที่
สิ่งสำคัญคือต้องทราบการตั้งค่าในระบบก่อนที่จะพิมพ์สตริงลงในวันที่ หากระบบเป็นไปตามรูปแบบวันที่ของสหราชอาณาจักร และหากเราระบุรูปแบบเวลาของสหรัฐอเมริกา อาจเป็นไปได้ว่าอาจเกิดข้อผิดพลาดหรือแสดงวันที่ไม่ถูกต้อง เช่น 16/07/2020 หมายถึงวันที่ 16 กรกฎาคมในรูปแบบ US ในขณะที่ในสหราชอาณาจักรเกิดข้อผิดพลาดเนื่องจากเดือนที่สองถือเป็นเดือนและเดือนต้องไม่มากกว่า 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)เอาต์พุต:
บทสรุป
ดังนั้นบทความนี้จึงกล่าวถึงรายละเอียดเกี่ยวกับการแปลงจากวัตถุสตริงเป็นวัตถุวันที่-เวลา โดยจะอธิบายรายละเอียดเกี่ยวกับวิธีการต่างๆ ในการแปลงสตริงเป็นออบเจ็กต์วันที่-เวลา วิธีแปลงออบเจ็กต์สตริงเป็นรูปแบบวันที่-เวลาที่ระบุ นอกจากนี้ยังอธิบายรายละเอียดข้อผิดพลาดที่อาจเกิดขึ้นระหว่างการแปลงและวิธีแก้ไข นอกจากนี้ ควรเรียนรู้เกี่ยวกับรูปแบบของระบบปัจจุบันก่อนการแปลง หากต้องการเรียนรู้รายละเอียดเพิ่มเติม ขอแนะนำให้เขียนโปรแกรมตัวอย่างและฝึกฝน







