Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Convert a string to date/time format

Reply
 
Old 04-23-2009   #1 (permalink)
Jobbsy


 
 

Convert a string to date/time format

I've looked at various threads and tried various suggestions

I have a string:

$str = "Apr 23 14:16"
I would like to convert this into a date time format variable so I can
perform a get-date and compare the two

i.e. if string is -gt 3minutes older than get-date then do something

Any help would be greatly appreciated

Many Thanks
--
jobbsy@xxxxxx

My System SpecsSystem Spec
Old 04-23-2009   #2 (permalink)
Vadims Podans [MVP]


 
 

Re: Convert a string to date/time format

you can use ParseExact static method for DateTime:

$str = "Apr 23 14:16"
[datetime]::ParseExact($str, "MMM dd HH:mm", $null)
MMM - month in string format
dd - day
HH - time in 24h format (hh - in 12h format with AM/PM appendix)
mm - minutes.

enjoy!
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Jobbsy" <Jobbsy@xxxxxx> rakstīja ziņojumā
"news:57FADB60-70C1-4D73-B89B-FFDFC3E845FD@xxxxxx"...
Quote:

> I've looked at various threads and tried various suggestions
>
> I have a string:
>
> $str = "Apr 23 14:16"
> I would like to convert this into a date time format variable so I can
> perform a get-date and compare the two
>
> i.e. if string is -gt 3minutes older than get-date then do something
>
> Any help would be greatly appreciated
>
> Many Thanks
> --
> jobbsy@xxxxxx
My System SpecsSystem Spec
Old 04-23-2009   #3 (permalink)
Vadims Podans [MVP]


 
 

Re: Convert a string to date/time format

mm, I forget second part of your task.
$str = "Apr 23 14:16"
if ((Get-date).AddMinutes(-3) -gt ([datetime]::ParseExact($str, "MMM dd
HH:mm", $null))) {"do something"}

have a nice day!
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Vadims Podans [MVP]" <vpodans> rakstīja ziņojumā
"news:#RqczMDxJHA.4476@xxxxxx"...
Quote:

> you can use ParseExact static method for DateTime:
>
> $str = "Apr 23 14:16"
> [datetime]::ParseExact($str, "MMM dd HH:mm", $null)
> MMM - month in string format
> dd - day
> HH - time in 24h format (hh - in 12h format with AM/PM appendix)
> mm - minutes.
>
> enjoy!
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog - www.sysadmins.lv
>
> "Jobbsy" <Jobbsy@xxxxxx> rakstīja ziņojumā
> "news:57FADB60-70C1-4D73-B89B-FFDFC3E845FD@xxxxxx"...
Quote:

>> I've looked at various threads and tried various suggestions
>>
>> I have a string:
>>
>> $str = "Apr 23 14:16"
>> I would like to convert this into a date time format variable so I can
>> perform a get-date and compare the two
>>
>> i.e. if string is -gt 3minutes older than get-date then do something
>>
>> Any help would be greatly appreciated
>>
>> Many Thanks
>> --
>> jobbsy@xxxxxx
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
RE: How to convert Table-Format Data to CSV-Format? PowerShell
RE: How to convert Table-Format Data to CSV-Format? PowerShell
Birthday field date and time format Vista mail
Converting date time format to string format PowerShell
Photo Gallery Date/Time Format Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46