PowerShell Dizeyi Tarihe Dönüştürme | En İyi 5 PowerShell Örneği
PowerShell Dizeyi Tarihe Dönüştürmeye Giriş
Çoğu durumda, bir dizenin tarih değişkenine veya tarih-saat nesnesine dönüştürülmesi gerekebilir. PowerShell'de bu birçok şekilde yapılabilir. Bunun, tip dökümü kullanılarak, tarihin ABD formatında alınmasıyla, Date Time'ın ayrıştırılmasıyla yapılabileceği yollardan bazıları. Bu dönüşüm, bir dize nesnesinin DB tablosuna gönderilmesi gibi birçok amaç için gereklidir; bu nedenle, kullanıcıdan girdi bir dize biçiminde alındığında ve daha sonraki işlemler için biçimlendirilmesi gerektiğinde bir dönüşüm gereklidir. Bu makalede, bir dize nesnesinin tarih-saat nesnesine dönüştürülebileceği çeşitli yollar ayrıntılı olarak ele alınacaktır.
Sözdizimi:
Aşağıda Datetime Ayrıştırmanın sözdizimi verilmiştir:
[Datetime]::ParseExact('07/15/2019', 'MM/dd/yyyy', $null)İlk parametrenin dönüştürülmesi gereken dize olduğu durumlarda, ikinci parametre üretilecek tarihin biçimidir ve üçüncüsü boş bir karakterdir.
Kod:
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Çıktı:
PowerShell'de Mevcut Tarih Formatı Türleri
Dizenin bir veri nesnesine dönüştürülebileceği çeşitli yolları öğrenmeden önce PowerShell'de bulunan çeşitli tarih türleri hakkında bilgi edinmek önemlidir. Dönüşüm sırasında faydalı olacaktır.
PowerShell'de kullanılabilen çeşitli tarih biçimleri aşağıdadır.
d: ShortDate'i belirtir
D: Uzun Tarih'i belirtir
f: Uzun tarihi, kısa zamanı belirtir
F: Uzun tarihi, uzun zamanı belirtir
t: Kısa süreli biçimi belirtir
T: Uzun süreli biçimi belirtir
m, M: Ay Gün biçimi
g: Genel veri zamanı kısa biçimini belirtir
G: Genel veri süresi uzun biçimini belirtir
Y veya y: Yıl ay modelini belirtir
PowerShell Dizeyi Tarihe Dönüştürme Örnekleri
Aşağıda PowerShell örnekleri verilmiştir:
Örnek #1
Kod:
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Çıktı:
Örnek #2
Kod:
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)
Çıktı:
Örnek #3
Kod:
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Çıktı:
Tarihle çalışırken dikkat edilmesi gereken önemli nokta
Bir dizeyi tarihe dönüştürmeden önce sistemin yerel ayarını bilmek önemlidir. Sistem Birleşik Krallık tarih formatını izliyorsa ve ABD saat formatını belirtirsek, bu durumda ya bir hata oluşması ya da yanlış günün görüntülenmesi mümkündür. Örneğin, 16.07.2020 ABD formatında 16 Temmuz'u belirtirken Birleşik Krallık'ta saniyenin ay olarak kabul edilmesi ve ayın 12'den büyük olamayacağı için hata oluşturur. Bunun üstesinden gelmek için yerel kültürel ortamlar kullanılabilir.
Örnek #4
Kod:
Write-Host "Local settings demo"
$string="13/2/2020"
$date= [DateTime] $stringÇıktı:
Yukarıdaki çıktıda sistem ABD saat dilimi ve ay önce geldiği için hata atılıyor yani 12'den büyük olamaz. 13'ü 12'ye değiştirirsek aşağıdaki çıktı üretiliyor.
Kod:
Write-Host "Local settings demo"
$string="12/2/2020"
$date= [DateTime] $string
Write-Host $date -ForegroundColor DarkYellowÇıktı:
Örnek #5
Kod:
$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)Çıktı:
Çözüm
Böylece makale, bir dize nesnesinden tarih-saat nesnesine dönüşüm konusunu ayrıntılı olarak ele aldı. Bir dizeyi tarih-saat nesnesine dönüştürmenin çeşitli yollarını, bir dize nesnesinin belirli bir tarih-saat biçimine nasıl dönüştürülebileceğini ayrıntılı olarak açıkladı. Ayrıca dönüşüm sırasında karşılaşılabilecek hataları ve bunların nasıl aşılacağını da detaylı bir şekilde anlattı. Ayrıca dönüştürmeden önce mevcut sistemin formatını öğrenmek de akıllıca olacaktır. Daha detaylı bilgi edinmek için örnek programlar yazıp bunları uygulamanız tavsiye edilir.







