Solved

Formatting a date/time with PowerShell

Posted on 2016-08-01
4
18 Views
Last Modified: 2016-08-01
I have a date/time like the below. I have tried different methods but cannot get it formatted properly. Can someone point me in the right direction?

Current Format
20160801164123.157000+000

Desired Format

yyyyMMdd HH:mm:ss
0
Comment
Question by:Lorrec
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41737872
([WMI]'').ConvertToDateTime('20160801164123.157000+000').ToString('yyyyMMdd HH:mm:ss')

Open in new window

1
 
LVL 45

Expert Comment

by:aikimark
ID: 41737920
@Lorrec

How important are the fractions of a second in your conversion?  The fractional seconds are being ignored by the posted solution as well as this alternative solution.
[System.Management.ManagementDateTimeConverter]::ToDateTime('20160801164123.157000+000').ToString('yyyyMMdd HH:mm:ss')

Open in new window


Note: I find some of these methods rather verbose.  I prefer something like this:
$DTC=[System.Management.ManagementDateTimeConverter]
$DTC::todatetime('20160801164123.957000+000').ToString('yyyyMMdd HH:mm:ss')

Open in new window

0
 

Author Comment

by:Lorrec
ID: 41737929
Thank you for the assistance. This was helpful.
0
 

Author Comment

by:Lorrec
ID: 41737933
Thank you aikimark for the assistance as well.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question