convert given string datetime2(7) in powershell - 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")

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")

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.

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.

Can't convert value from datatable to datetime

Using powershell 5.1
I have a function called that returns a string representing a date like this
"4/22/2019 12:00:00 AM"
function Get-LastLogTime() {
$lastRunDate = Get-SQLData "." "AdHoc" "SELECT TOP 1 o.LogTime FROM dbo.FAQlog o WHERE o.RecordsSent = 1 ORDER BY o.LogTime DESC"
return $lastRunDate
}
Where LogTime is the usual Datetime SQL type and Get-SQLData is another function that returns a datatable.
If I just check the return value, I get something like this
LogTime
-------
4/22/2019 12:00:00 AM
Ok, great, but I need to compare this date to the current date. So, I do something like this but i get an error on the line trying to convert $testDate to datetime.
# test
$testDate = Get-LastLogTime
([DateTime]$testDate) -lt (Get-Date)
If I just do a simple comparison at the command line, it works, eg.
([DateTime]"4/22/2019 12:00:00 AM" ) -lt (Get-Date)
It works with
([DateTime]"4/22/2019 12:00:00 AM" )
Because you are casting DateTime on a String.
Return value of Get-LastLogTime is not a string probably and casting it to a Datetime does not work.
get-LastLogTime | get-member will show you the methods you have available on the returned data type such as, for instance, ToString, and that can be used to perform the comparison.

How to convert datetime to string or double in 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)