Aman Kakkar
Aman Kakkar

Reputation: 61

Powershell - Adding milliseconds to timestamp

I am trying to create a timestamp string in the format:

yyyymmddhhmmssfff

where f is milliseconds.

Example: 20171013180359235

So far I have

[string]$Date = Get-Date -UFormat "%Y%m%d%H%M%S"

With this I get only up to the seconds. I know if I add %l to the end I get a precision of 2 milliseconds, but I am one short. Is there any way to describe how precise I can choose the milliseconds. Thanks

Upvotes: 2

Views: 9705

Answers (3)

user2.7182818284
user2.7182818284

Reputation: 11

The hour format specifier is case sensitive - lower case uses 12 hour clock and uppercase 24 hours. I suspect you would prefer 24 hour. So 'yyMMddHHmmssfff'

Upvotes: 0

tommymaynard
tommymaynard

Reputation: 2152

I use this for some of my work (think, naming files): Get-Date -Format 'DyyyyMMddThhmmsstt.fffffff'. The capital D is for Date and the capital T for time. Perhaps this is helpful!

D20171013T101807AM.8629943

Upvotes: 1

mjolinor
mjolinor

Reputation: 68263

Using the .tostring() method of the datetime object:

 (get-date).ToString('yymmddhhmmssfff')

171513121549340

Upvotes: 7

Related Questions