Home > Net >  Join date and time as datetime format and pass it in schedule trigger powershell
Join date and time as datetime format and pass it in schedule trigger powershell

Time:02-04

I have below date and time in 2 separate variable. I am trying to combine these 2 as date time and create a scheduler based on that but getting error

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:2 char:1
  $dt1 = [datetime]::ParseExact($dt, 'g',[CultureInfo]::InvariantCultur ...
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      FullyQualifiedErrorId : FormatException

Below is my code

$L_Friday="25/02/2022"
$dt = $L_Friday   " "   "10:00:00 AM"
$dt1 = [datetime]::ParseExact($dt, 'dd/MM/yyyy hh:mm:ss',[CultureInfo]::InvariantCulture)
$Trigger = New-ScheduledTaskTrigger -Once -At ""

Please need your help to get the issue here

CodePudding user response:

Your code will be like this:

$L_Friday="25/02/2022"
$dt = $L_Friday   " "   "10:00:00 AM"
$dt1 = [datetime]::ParseExact($dt, 'dd/MM/yyyy hh:mm:ss tt' [CultureInfo]::InvariantCulture)
echo $dt1

For more details, you can check this StackOverflow Question and Answer.

  •  Tags:  
  • Related