Powershell - Adding milliseconds to timestamp - powershell

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

Using the .tostring() method of the datetime object:
(get-date).ToString('yymmddhhmmssfff')
171513121549340

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

Related

How to format Get-Date Day as 2 digits in Powershell

I try to get the Day as a two digit number out of Get-Date in PowerShell.
When I try (Get-Date).Day the result will be for example 6 but I want to have it as 06. Also for the month.
How can this be done?
I have already tried things like (Get-Date).Day.ToString("dd") but it doesn't work.
Using ToString() and supplying date formatters (e.g. "yyyy" or "dd") will only work on dates. By accessing .Day or .Year, the operation is instead attempted on an integer, which will fail.
Try (for the day):
(Get-Date).ToString("dd")
...and (and for the month):
(Get-Date).ToString("MM")
See here for custom formatting of dates using ToString()

How to get the previous hour time with format in powershell?

I need to find the previous hour time with formatting with PS:
I need the below format:
"yyyy-MM-dd-HH:mm:sstt")
I can use the below code to get the date and time in this format:
(Get-Date -Format "yyyy-MM-dd-HH:mm:sstt")
2019-09-17-08:45:27AM
I need to get the previous hour time but in the above format
I know how to get the last hour time :
(Get-Date).AddHours(-1)
How can i get the previous hour time with a combination of the above
Format?
Using -f, the format operator, as shown in Ivan Mirchev's helpful answer is definitely an option, and -f is a great general-purpose formatting option to know about, for any data type.
However, in your particular case there is a simpler solution, because the .ToString() method of [datetime] instances directly accepts a format string:
(Get-Date).AddHours(-1).ToString('yyyy-MM-dd-HH:mm:sstt')
You may try using the format operator:
"{0:yyyy-MM-dd-HH:mm:sstt}" -f (get-date).AddHours(-1)
more details: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#format-operator--f
Hope it helps! :)

How do I convert a PowerShell datetime string from 24 hour to 12 hour format?

The actual line in the PowerShell script that is desired is:
$tsd = [datetime]::ParseExact($TSDiff,'yyyyMMddhhmmsstt',$null)
But the $TSDiff variable being used has time expressed as, without AM/PM:
20171023212800
This is a 24-hour format where 11 pm is represented by 23. It was retrieved using an FTP request which seems to only return 24 hour format strings without AM/PM.
Breaking this down, the following PowerShell command works:
[datetime]::ParseExact("20171023092800",'yyyyMMddhhmmss',$null)
But the following PowerShell command does not work:
[datetime]::ParseExact("20171023212800",'yyyyMMddhhmmss',$null)
The reason the second line doesn't work is clear; the hour digits are in 24-hour format, as in the $TSDiff listed at the beginning of this post.
Is there a simple way in PowerShell to convert the string 20171023212800 to 20171023092800PM?
From Formatting Dates and Times
[...]
h, %h - The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify %h if the format pattern is not combined with other format patterns.
hh - The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H, %H - The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify %H if the format pattern is not combined with other format patterns.
HH - The hour in a 24-hour clock. Single-digit hours will have a leading zero.
[...]
While you are converting your datetime string to a 12-hour formatted string with hh in the format specifier, it will convert to a 24-hour string with HH in it like:
[datetime]::ParseExact("20171023212800",'yyyyMMddHHmmss',$null)
Use:
# Method 1. Use HH for 24-hour format like TessellatingHeckler proposes
[datetime]::ParseExact("20171023212800", 'yyyyMMddHHmmss', $null)
# Method 2. If you are not sure your string is
# date, use TryParse with the same format
[datetime]$dirDate = New-Object DateTime
if ([DateTime]::TryParseExact(
'20171023212800',
'yyyyMMddHHmmss',
[System.Globalization.CultureInfo]::InvariantCulture,
[System.Globalization.DateTimeStyles]::None,
[ref]$dirDate))
{
$dirDate
}

Add nanosecond resolution unix time

I am currently converting a date time value to unix time so that it can be inserted into a time series database (influxdb) using the following code:
(Get-Date -Date $_.timecreated -UFormat %s)
Unfortunately influx requires nano second resolution while the above gives me ms resolution. What is the best way to add nano second resolution that influx can accept as a valid unix timestamp? I have tries adding some 0000 characters at the end but that does not always work.
You will have to compute the unix timestamp yourself like this, by computing the offset from the unix epoch:
$utime = ((Get-Date -Date $_.timecreated) - (Get-Date "1/1/1970")).TotalSeconds
that gives you a double that you can format as you like. This will give nanosecond precision:
"{0:F06}" -f $utime
Update: It's important to note that I don't think Get-Date will give you a nanosecond precision of the time. On my machine the nanosecond place value is always 0.

Formatting date month in PowerShell

I need to format the previous date to yyyy-mm-dd.
For example, today is May 25,2016. Therefore I need the output '2016-05-24'.
$a = (Get-Date).AddDays(-1).ToString('yyyy-mm-dd')
Write-Output $a
When I run the code I get 2016-44-24 which is incorrect.
How can I do it?
mm specifies The minute, from 00 through 59
Source.
You have to use uppercase for month instead:
(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')