How to convert datetime to string or double in powershell - powershell

I would like to get a hint how to convert datetime -uFormat %T (for example 08:45:00) to double or a string like 8,75 hours.
Thank you very much in advance!

$date = Get-Date
'{0:HH},{1:N0} hours' -f $date,($date.Minute/60*100)

Related

talend format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

I am trying to change the date format in txmlmap component but its not working
i want change date format
from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
expected output:- yyyy-mm-dd HH:mm:ss
You can parse your string to a date using your source pattern and then format that date to a string using your target pattern:
TalendDate.formatDate("yyyy-mm-dd HH:mm:ss", TalendDate.parseDate("yyyy-MM-dd'T'HH:mm:ss.SSSz", myDateString))
In almost all coding languages format is text, while date is a double. That means you must first make a date of the first expression, before setting the new format of that date. But in Your case the 'T' is some kind of special format that need to be replaced with a blanck space. I have no idea about what it would look like in talend but in VB it would look like this:
' from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
DateTxt = "2022-12-01'T'22:45:10"
DateTxt = Replace(DateTxt, "'T'", " ")
MyDate = CDate(DateTxt)
MsgBox Format(MyDate, "yyyy-mm-dd HH:mm:ss")

How to load datetime value from a string in FileDateTime format in powershell?

Get-Date -Format FileDateTime
gives something like this:
20211027T1306219297
Is it possible to import such a string into a datetime variable?
Something like?
$date = Get-Date "20211027T1306219297"
You can use [datetime]::ParseExact method with yyyyMMddTHHmmssffff as format argument.
$date = [datetime]::ParseExact("20211027T1306219297", 'yyyyMMddTHHmmssffff', $null)
This is because FileDateTime internally uses yyyyMMddTHHmmssffff format which is documented here.
FileDateTime.
A file or path-friendly representation of the current date and time in
local time, in 24-hour format. The format is yyyyMMddTHHmmssffff
(case-sensitive, using a 4-digit year, 2-digit month, 2-digit day, the
letter T as a time separator, 2-digit hour, 2-digit minute, 2-digit
second, and 4-digit millisecond). For example: 20190627T0840107271.

PowerShell time conversion to UTC from a GMT+1 string

i use PowerShell for scripting some stuff.
Actual i got a string from a logfile - this one i prepared a timestamp like this example:
$timestamp = $timestampDate + " " + $timestampTime
#timestamp: 2020.11.16 06:03:27
This timestamp is a GMT+1 timestamp from my timezone but i need it in UTC (wintertime).
So i try:
get-date('2020.11.16 06:03:27') -f "yyyy.MM.dd hh:mm:ss z"
2020.11.16 06:03:27 +1
Get-Date('2020.11.16 06:03:27') -Format FileDateTimeUniversal
20201116T0503270000Z
Now i try how i could format the result to 2020.11.16 05:03:27 in a easy way without string manipulation (my DB field is without timezone)
Thanks a lot.

convert given string datetime2(7) in powershell

I am getting date time in string format 2020-08-19T08:00:53.643Z .
I need to convert this into datetime2(7) format i.e 2020-01-20 21:10:11.4866667.
how can I do that with powershell
currently I am using this
$lastExectedTime= Get-Date $temp -Format "yyyy-MM-dd HH:mm:ss.fffffff"
I am using something similar to Daniel Björks answer (using Parse method from [datetime])
C:\> ([datetime]::Parse("2020-08-19T08:00:53.643Z")).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
2020-08-19 10:00:53.6430000
You can do like this:
([DateTime]"2020-08-19T08:00:53.643Z").ToString("yyyy-MM-dd HH:mm:ss.fffffff")

How to convert powershell UTC datetime object to EST

I have date time strings coming in, formatted like the following:
2017-08-03T12:30:00.000Z
I need to be able to convert these to EST. Every function I have tried throws one error or another, typically being:
"String was not recognized as a valid DateTime."
I have tried variations of the below:
$time = '2017-08-03T12:30:00.000Z'
[datetime]$datetime = $time
$culture = [Globalization.CultureInfo]::InvariantCulture
[DateTime]::ParseExact($datetime, 'MM-dd-yyyy HH:mm:ss', $culture)
I think it has something to do with how the Date Time string I am referencing has the **T** and then the UTC time, but can't figure out what to do about it. Maybe I should parse out the time, convert it and then reattach to the first part of the string, the date, and combine them together for the final output? Seems like way too much work and a solution which would cause potential errors in the future.
You should be able to convert a Zulu time string to a DateTime value simply by casting it. However, the resulting value will be in local time, so you should convert it back to UTC for further calculations:
$timestamp = '2017-08-03T12:30:00.000Z'
$datetime = ([DateTime]$timestamp).ToUniversalTime()
Then you can use the TimeZoneInfo class to convert the UTC timestamp to the desired timezone:
[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($datetime, 'Eastern Standard Time')
Use [TimeZoneInfo]::GetSystemTimeZones() | Select-Object Id, DisplayName to get a list of the recognized timezones.
Try using the static ConvertTimeBySystemTimeZoneId() method of the [System.TimeZoneInfo] class:
$time = '2017-08-03T12:30:00.000Z'
$result = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date -Date $time), 'Eastern Standard Time')
The returned $result is a [DateTime].
BTW, if you ever need to convert it back, here's how:
Get-Date -Date $result -Format FileDateTimeUniversal
Hope this helps.