• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Converting result of the output to needed format

I get the output of the variable $WinInsDate in the format "20120509120504.000000 +180"

how can I convert it to a format similar to output command

 Get-Date -DisplayHint date ?
0
nesher13
Asked:
nesher13
  • 4
  • 3
1 Solution
 
footechCommented:
Where are you getting that date from?  Looks like time from some WMI queries.
$a = (Get-WmiObject Win32_OperatingSystem)
$a.converttodatetime($a.installdate)

Open in new window


See here for a little background info.
http://technet.microsoft.com/en-us/magazine/2008.12.heyscriptingguy.aspx?pr=PuzzleAnswer
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
WMI dates don't have the additonal space between time and timezone offset (in minutes). Either the format above is wrong, or there is a different source ...

If you want to do it the hard, but most flexible way:
invoke-expression ( "20120509120504.000000 +180" -replace '^(\d{4})(\d{2})(\d{2})(\d\d)(\d\d)(\d\d)\.\d+ \+(\d+)', '(get-date -date "$2/$3/$1 $4:$5:$6").AddMinutes($7)')

Open in new window

I've ignored the milli/microseconds stuff.
0
 
nesher13Author Commented:
to footech

how can I convert the resulting format into a format mm/dd/yyyy ?
0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Both expressions will result in a datatime, which again can be formatted by e.g. stuffing into get-date -format .... or using the -f operator (which will automatically use the date format set up in the OS):
  get-date -format 'MM/dd/yyyy' $yourDateTime
   '{0:d}' -f $yourDateTime
0
 
nesher13Author Commented:
I found a pretty simple solution to the second part of the question

PS C:\>$a = (Get-WmiObject Win32_OperatingSystem)

PS C:\>$a.converttodatetime($a.installdate)

Sunday, August 04, 2013 5:46:45 PM

PS C:\>$a.converttodatetime($a.installdate) | get-date -format d

8/4/2013
0
 
nesher13Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for nesher13's comment #a39520350
Assisted answer: 500 points for footech's comment #a39517538

for the following reason:

footech answer gave me the basis for solving the problem
0
 
nesher13Author Commented:
footech answer gave me the basis for solving the problem
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
So your date literal "20120509120504.000000 +180" was wrong? The corresponding WMI date literal is "20120509120504.000000+180" (no space, as mentioned above).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now