You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell GUI获取选中日期与时间至变量的问题求助

Fix: Get Both Selected Date and Time from PowerShell GUI

Got it, let's adjust your script to capture both the selected date and time, then store them in variables for your use. The issue with your original code is that you're only retrieving the value from the date picker control—we just need to add logic to grab the time picker's value too.

Here's the modified code:

Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$font = New-Object System.Drawing.Font("Consolas", 13)
$mainForm.Text = "Pick Time Frame"
$mainForm.Font = $font
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkOliveGreen"
$mainForm.Width = 300
$mainForm.Height = 200

# DatePicker Label
$datePickerLabel = New-Object System.Windows.Forms.Label
$datePickerLabel.Text = "date"
$datePickerLabel.Location = "15, 10"
$datePickerLabel.Height = 22
$datePickerLabel.Width = 90
$mainForm.Controls.Add($datePickerLabel)

# MinTimePicker Label
$minTimePickerLabel = New-Object System.Windows.Forms.Label
$minTimePickerLabel.Text = "min-time"
$minTimePickerLabel.Location = "15, 45"
$minTimePickerLabel.Height = 22
$minTimePickerLabel.Width = 90
$mainForm.Controls.Add($minTimePickerLabel)

# DatePicker
$datePicker = New-Object System.Windows.Forms.DateTimePicker
$datePicker.Location = "110, 7"
$datePicker.Width = "150"
$datePicker.Format = [windows.forms.datetimepickerFormat]::custom
$datePicker.CustomFormat = "dd/MM/yyyy"
$mainForm.Controls.Add($datePicker)

# MinTimePicker
$minTimePicker = New-Object System.Windows.Forms.DateTimePicker
$minTimePicker.Location = "110, 42"
$minTimePicker.Width = "150"
$minTimePicker.Format = [windows.forms.datetimepickerFormat]::custom
$minTimePicker.CustomFormat = "HH:mm:ss"
$minTimePicker.ShowUpDown = $TRUE
$mainForm.Controls.Add($minTimePicker)

# OK Button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = "15, 130"
$okButton.ForeColor = "Black"
$okButton.BackColor = "White"
$okButton.Text = "OK"
$okButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($okButton)

[void] $mainForm.ShowDialog()

# Capture selected values into variables
$selectedDate = $datePicker.Value.Date  # Isolates just the date part
$selectedTime = $minTimePicker.Value.TimeOfDay  # Isolates just the time part
$combinedDateTime = $selectedDate.Add($selectedTime)  # Merges date and time into one DateTime object

# Output results
Write-Host "Selected Date: $selectedDate"
Write-Host "Selected Time: $selectedTime"
Write-Host "Combined Date & Time: $combinedDateTime"

# Optional: Format the combined value to match your preferred string format
$formattedDateTime = $combinedDateTime.ToString("dd/MM/yyyy HH:mm:ss")
Write-Host "Formatted DateTime: $formattedDateTime"

What Changed:

  • After the form closes, we now access both $datePicker.Value and $minTimePicker.Value to get user selections.
  • We split the values into separate variables ($selectedDate and $selectedTime) if you need them individually, or combine them into a single DateTime object for easier use.
  • Added formatted output so you can view the values in a clean, customizable string format.

Now when you run the script, you'll see both the selected date and time displayed, and they'll be stored in variables you can use elsewhere in your PowerShell workflow.

内容的提问来源于stack exchange,提问作者Varun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:32:38