使用powershell进行日历格式化

时间:2016-02-04 03:49:05

标签: powershell

下面的代码将从日历中快速选择日期,但不是我想要格式的格式(yyyy,MM,dd):     Add-Type -AssemblyName System.Windows.Forms     Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form 

$form.Text = "Select a Date" 
$form.Size = New-Object Drawing.Size @(243,230) 
$form.StartPosition = "CenterScreen"

$calendar = New-Object System.Windows.Forms.MonthCalendar
Get-MonthCalendar -Bold $WeekendDays -YBold:yyyy,MM,dd   
$calendar.ShowTodayCircle = $False
$calendar.MaxSelectionCount = 1
$form.Controls.Add($calendar) 

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(38,165)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(113,165)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$form.Topmost = $True

$result = $form.ShowDialog() 

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$path = "H:\oim\adcbsm007\adcbsm007.txt"
$word = "yyyy,MM,dd"
$replacement = $calendar.SelectionStart
$text = get-content $path
$newText = $text -replace $word,$replacement
$newText > "H:\oim\adcbsm007\karthik.txt"

}

1 个答案:

答案 0 :(得分:0)

使用字符串方法

$replacement.ToString('yyyy,MM,dd')