TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Date time calculations

Microsoft Windows Powershell

 
 
Thread Tools Display Modes
Unread 18-04-2008, 06:23 PM   #1
Snowmizer
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.

 
Unread 18-04-2008, 06:23 PM   #2
Brandon Shell [MVP]
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>


 
Unread 18-04-2008, 06:23 PM   #3
Brandon Shell [MVP]
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>


 
Unread 18-04-2008, 06:23 PM   #4
Marco Shaw [MVP]
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
 
Unread 18-04-2008, 06:23 PM   #5
Snowmizer
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>
>
>
>

 
Unread 18-04-2008, 06:23 PM   #6
Snowmizer
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
>

 
Unread 18-04-2008, 06:24 PM   #7
Brandon Shell [MVP]
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>



 
Unread 18-04-2008, 06:24 PM   #8
Brandon Shell [MVP]
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>



 
Unread 18-04-2008, 07:23 PM   #9
Snowmizer
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
> >

 
Unread 18-04-2008, 07:23 PM   #10
Marco Shaw [MVP]
Guest
 
Posts: n/a
Re: Date time calculations

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
 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 10:08 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional