Closed doctordns closed 3 years ago
Seems like it would be nice to sync this up with a new PSKoans foundations learning block on datetime and datetimeoffset along with datetime math ($_.LastWriteTime -gt (Get-Date).AddDays(-1)
)
Should also definitely link to the docs on those classes.
Thanks for trhe comments.
It is my intention that each post should contain a bit of background to the question. So in this case, this article should talk about system.datetime
class (and properties, as well as the Get-Date
cmdlet.
@Szeraax I am not personally familiar with PSKoans - feel free to suggest how to integrate it. Seems a great idea.
I think that we would need to create a new PSKoan script module and then ask Vexx to add it to the module. I like that Hashtable is in the foundations area and think datetime would fit too. Maybe DateTimeOffset and TimeSpan aren't as important and should be cut out.
I just spent a few minutes creating a really quick partial sample of what it could be. Someone completing this module would need to edit all the instances of $__
to something else that makes the unit tests not fail:
using module PSKoans
[Koan(Position = 122)]
param()
<#
Dates
In this module, you will test your understanding of DateTimes,
TimeSpans, DateTimeOffset's, and date/time math.
A date/time is called a datetime and points to a local time. Each
datetime object points to a specific time, but each DateTimeOffset
object points to a specific global moment in time.
You will need to understand the ramifications of both to complete
these challenges.
#>
Describe 'Datetimes' {
It 'allows you to point to a time' {
<#
A DateTime object can be created many ways
Refer to MS Docs for more help:
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.-ctor
#>
[datetime]"$__" | Should -Be (Get-date 2000-01-01).Date
[datetime]::new($__) | Should -Be (Get-Date 2001-02-03).Date
}
It 'can be compared and operated on' {
# Which one, which one
# Does it really matter?
$One = [datetime]"2020-01-01"
$Two = [datetime]"2020-01-02"
$__ -gt $__ | Should -Be $true
$__.AddDays(-7) -lt $__ | Should -Be $true
}
}
Describe 'TimeSpans' {
It 'is a length of time' {
[timespan]"$__" | Should -Be [timespan]::New(2, 15, 3)
}
# Add more
}
Describe 'DateTimeOffsets' {
It 'is always the same instant' {
# Add more
}
# Add more
}
As this article is now up on the blog, I will close this PR.
I don't want to lose the comment regarding PSKoans. Can I suggest that @Szeraax post the issue and suggestion to their issue page
Summary of the update request
How can I get yesterday's date?
https://devblogs.microsoft.com/scripting/how-can-i-get-yesterdays-date/
I see a lot of questions like this
Needs to be re-written for PowerShell
The article should explain the DateTime class plus show how to use the methods.