Blog Post

A Little Current Date Arithmetic in Powershell–#SQLNewBlogger

,

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

I saw a fun post on Twitter recently asking days until retirement. I wrote this code:

DECLARE @YearsToRetire INT = 11;
SELECT DATEDIFF (DAY, GETDATE (), (DATEADD (YEAR, @YearsToRetire, GETDATE ())));

I thought that wasn’t bad, but then I wondered, how would I do this in PowerShell? I knew there had to be a way, so I googled and ran into this article. That showed me Get-Date and the AddYears() method. So I tried this:

2022-01-05 10_26_03-C__Users_Steve

That didn’t work well. I ended up storing the date in a variable and then things worked. However, I was bothered. I can pipe and combine things, why didn’t this work?

Then I thought about something. I ought to enclose the Get-Date inside something. I tried parens, like this:

(Get-Date).AddYears(11)

That worked great. As I get more familiar with PoSh, I can figure more things out myself, and quicker.

2022-01-05 10_28_06-C__Users_Steve

SQLNewBlogger

Once I figured out the way to do this, I spent about 5 minutes on this post. Actually 6 minutes with this part added.

I took something I figure out and wrote this up quickly. I even added a great line at the end, that I figured it out and my work with the language is paying dividends.

A great little type of post that would highlight your blog.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating