![]() |
|
|
|
#1 |
|
Guest
Posts: n/a
|
Date time calculations
I am trying to use the schtasks program to schedule a task using Powershell.
I need to calculate the start time for the scheduled task, add one minute and schedule the second task I need to run. I have the following code in my script: $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))$ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = $ShutdownDateTime.addminutes(1) $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') When I run my script everything appears to work however when I look at the time in $ShutdownTime it is not right. Example: $CurrDateTime = "04/18/2008 11:24" $ShutdownDateTime = "04/18/2008 11:25" $ShutdownTime = "11:04" What is the best way to be able to calculate the proper time in HH:MM format? Thanks. Advertisement |
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Date time calculations
Try this
$ShutdownTime = $ShutdownDateTime.toString("HH:MM") Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject S> $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))S> $ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = S> $ShutdownDateTime.addminutes(1) S> S> $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') S> |
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Date time calculations
btw... If MM is 2 day month... if you want minutes (which I assume you do)
you need mm Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject S> $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))S> $ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = S> $ShutdownDateTime.addminutes(1) S> S> $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') S> |
|
|
#4 |
|
Guest
Posts: n/a
|
Re: Date time calculations
Snowmizer wrote:
> I am trying to use the schtasks program to schedule a task using Powershell. > I need to calculate the start time for the scheduled task, add one minute and > schedule the second task I need to run. I have the following code in my > script: > > $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))> $ShutdownDateTime = $CurrDateTime.addminutes(1) > $StartDateTime = $ShutdownDateTime.addminutes(1) > > $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') > > When I run my script everything appears to work however when I look at the > time in $ShutdownTime it is not right. Scratched my head too... I think you want: mm=minutes not MM=month (April = 4) ;-) -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
|
|
#5 |
|
Guest
Posts: n/a
|
Re: Date time calculations
No luck.
$ShutdownDateTime = "04/18/2008 12:56:39 PM" $ShutdownTime = 12:04 "Brandon Shell [MVP]" wrote: > btw... If MM is 2 day month... if you want minutes (which I assume you do) > you need mm > > Brandon Shell > --------------- > Blog: http://www.bsonposh.com/ > PSH Scripts Project: www.codeplex.com/psobject > > S> $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))> S> $ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = > S> $ShutdownDateTime.addminutes(1) > S> > S> $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') > S> > > > |
|
|
#6 |
|
Guest
Posts: n/a
|
Re: Date time calculations
Ok, so the command that Brandon recommended worked. It was a case sensitivity
thing on my part. Didn't realize that format would be case sensitive. Thanks guys for the help. "Marco Shaw [MVP]" wrote: > Snowmizer wrote: > > I am trying to use the schtasks program to schedule a task using Powershell. > > I need to calculate the start time for the scheduled task, add one minute and > > schedule the second task I need to run. I have the following code in my > > script: > > > > $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))> > $ShutdownDateTime = $CurrDateTime.addminutes(1) > > $StartDateTime = $ShutdownDateTime.addminutes(1) > > > > $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') > > > > When I run my script everything appears to work however when I look at the > > time in $ShutdownTime it is not right. > > Scratched my head too... > > I think you want: > mm=minutes > not > MM=month (April = 4) > > ;-) > > > -- > Microsoft MVP - Windows PowerShell > http://www.microsoft.com/mvp > > PowerGadgets MVP > http://www.powergadgets.com/mvp > > Blog: > http://marcoshaw.blogspot.com > |
|
|
#7 |
|
Guest
Posts: n/a
|
Re: Date time calculations
What is your Lang Set to?
Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject S> No luck. S> S> $ShutdownDateTime = "04/18/2008 12:56:39 PM" S> $ShutdownTime = 12:04 S> "Brandon Shell [MVP]" wrote: S> >> btw... If MM is 2 day month... if you want minutes (which I assume >> you do) you need mm >> >> Brandon Shell >> --------------- >> Blog: http://www.bsonposh.com/ >> PSH Scripts Project: www.codeplex.com/psobject >> S> $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))>> S> $ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = >> S> $ShutdownDateTime.addminutes(1) >> S> >> S> $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') >> S> |
|
|
#8 |
|
Guest
Posts: n/a
|
Re: Date time calculations
you did use lower case mm correct?
Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject S> No luck. S> S> $ShutdownDateTime = "04/18/2008 12:56:39 PM" S> $ShutdownTime = 12:04 S> "Brandon Shell [MVP]" wrote: S> >> btw... If MM is 2 day month... if you want minutes (which I assume >> you do) you need mm >> >> Brandon Shell >> --------------- >> Blog: http://www.bsonposh.com/ >> PSH Scripts Project: www.codeplex.com/psobject >> S> $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))>> S> $ShutdownDateTime = $CurrDateTime.addminutes(1) $StartDateTime = >> S> $ShutdownDateTime.addminutes(1) >> S> >> S> $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') >> S> |
|
|
#9 |
|
Guest
Posts: n/a
|
Re: Date time calculations
Ok, so one more question. The actual time 1:22 PM but the task is getting
scheduled as 1:22 AM. How do I make sure I get AM/PM correct? Thanks. "Snowmizer" wrote: > Ok, so the command that Brandon recommended worked. It was a case sensitivity > thing on my part. Didn't realize that format would be case sensitive. > > Thanks guys for the help. > > "Marco Shaw [MVP]" wrote: > > > Snowmizer wrote: > > > I am trying to use the schtasks program to schedule a task using Powershell. > > > I need to calculate the start time for the scheduled task, add one minute and > > > schedule the second task I need to run. I have the following code in my > > > script: > > > > > > $CurrDateTime = [datetime]: arse((Get-Date -Format 'HH:MM'))> > > $ShutdownDateTime = $CurrDateTime.addminutes(1) > > > $StartDateTime = $ShutdownDateTime.addminutes(1) > > > > > > $ShutdownTime = (Get-Date($ShutdownDateTime) -format 'HH:MM') > > > > > > When I run my script everything appears to work however when I look at the > > > time in $ShutdownTime it is not right. > > > > Scratched my head too... > > > > I think you want: > > mm=minutes > > not > > MM=month (April = 4) > > > > ;-) > > > > > > -- > > Microsoft MVP - Windows PowerShell > > http://www.microsoft.com/mvp > > > > PowerGadgets MVP > > http://www.powergadgets.com/mvp > > > > Blog: > > http://marcoshaw.blogspot.com > > |
|
|
#10 |
|
Guest
Posts: n/a
|
Re: Date time calculations
Advertisement Snowmizer wrote:
> Ok, so one more question. The actual time 1:22 PM but the task is getting > scheduled as 1:22 AM. How do I make sure I get AM/PM correct? > Did you also go with HH->hh? Maybe depending on your timezone/time config, but HH gives me "army" time (00-23)... Marco Advertisement |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |