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.
Related
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")
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.
Is it possible to compare 2 custom dates. Am trying check if variables hold date1 is lessthan date2, if so, report saying date1 is older date.
I getting both dates from a. date1 from log file and date2 from application itself
now, both date1 and date2 are in required format ie,
$Date1 = Tue,Aug 16, 2016 12:40:03
$Date2 = Mon,Aug 22, 2016 16:33:02
my next step is compare these 2 dates and report if date1 is older date compare to Date2, which I don't know how to proceed.. Any help/ideas is much appreciated.
Thanks to Pete and Ansgar Wiechers
updated working Code :
$Date1DateTime = [DateTime]::ParseExact($Date1,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date2DateTime = [DateTime]::ParseExact($Date2,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date1DateTime -lt $Date2DateTime
You can only compare date strings if the string sort order is the same as the date sort order. For instance, date strings in ISO format are comparable:
2016-08-16T12:40:03
2016-08-22T16:33:02
Date strings in your custom format are not, because T comes after M, but August 16 should actually come before August 22:
Tue,Aug 16, 2016 12:40:03
Mon,Aug 22, 2016 16:33:02
If you don't have the date strings in ISO format it's usually better to parse them into actual DateTime values (as #PetSerAl suggested), particularly if your reference value is originally a DateTime anyway.
$fmt = 'ddd,MMM d, yyyy, HH:mm:ss'
$culture = [Globalization.CultureInfo]::InvariantCulture
$Date1 = Get-Date $LogFileDate
$val = (b2b.exe -readparams $param | Select-Object -Skip 1 -First 1) -split '='
$Date2 = [DateTime]::ParseExact($val[1], $fmt, $culture)
if ($Date1 -lt $Date2) {
...
}
Using a Powershell date object how do you get the day number of the week, 0 to 6 where 0 would be Sunday and 6 would be Saturday. I know that I can get the day name with the code below but how do I get the number as there is no DayNumberOfWeek or equivalent property?
(Get-Date).DayOfWeek
I suppose I could use the day name from the code above in a switch statement to convert it to a number but that doesn't seem to be very eloquent.
like this:
( get-date ).DayOfWeek.value__
I suggest for the future to investigate what properties an object in this way:
( get-date ).DayOfWeek | gm -f # gm is an alias for get-member
Well, the DayOfWeek property of a DateTime is not a string but rather a DayOfWeek enum, so the shortest answer is probably
[Int] (Get-Date).DayOfWeek # returns 0 through 6 for current day of week
Or
[Int] [DayOfWeek] "Wednesday" # returns 3
Get-Date -UFormat %u
will return formated date.
check http://technet.microsoft.com/en-us/library/hh849887.aspx for more fomats
On Friday the 17th:
(get-date).DayOfWeek.value__
Returns 5
(Get-Date).DayOfWeek
Returns Friday
(Get-Date).Day
Returns 17
I have a table with a YearMonth column (201302) in it.
I created YearMonth a parameter.
Now I would like to write an expression in SSRS so that every time the report runs the date and month would look like this Feb-2013.
Could you please suggest me why following expression did not work.
Thanks
=CStr(Right(MonthName(Month(Parameters!NLR_YearMonth.Value)),3))
+ "-" +
Left(Year(Parameters!NLR_YearMonth.Value),4)
Try This:
=Format(DateValue(MonthName(Right(Parameters!NLR_YearMonth.Value, 2))
+ "," +
Left(Parameters!NLR_YearMonth.Value,4)), "Y")
The expression goes as follows:
Cutting the string to get the month.
Converting the mount to MountName
Creating a comma seperator between the month and the year
Cutting the string to get the year
Converting the returns string to Date object using the DateValue function
Formatting the Date to fits your needs
The results should be:
February, 2013