PowerShell Quick Tips : Get-Date

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this Powershell quick tip video this quick tip video is actually going to be on the get date commandlet so its primary focus is really getting the current date time on your local system but there are actually a lot of different parameters that we can use use with the get date to get us very specific information or to get us even the universal time and we can also create timestamps and get these specific day of year with some of the different methods that we can do on date time variables so let's actually go ahead and let's take a little bit of a deeper dive on the get date conveyent to see how we can make it maybe a little bit more useful for our Scripts so by default the get Dash date commandlet we just call it by using the get Dash date here gives us the current date time of our local system which for me right now is November 13th 605 35 PM so we get the date we get the time uh in a standard 12 hour Am Pm format now what we can actually do is just get the actual date or just get the time sometimes in our script maybe we don't care about the actual time we only care about the date we only need the date or maybe sometimes in our scripts we don't care about what date it is because it's something that we need to do every day but we care about the time that it is so to do that is a get date and then there's a parameter called display hint and in display hint you'll actually be prompted for three different options date which is just going to get you the date date time which is going to get you both the date and the time which is the default and then just time so if we actually just do date here and we actually run this we get just November 13th 2022 if we just do time we will actually only get 606 45 PM which is the current time so that is something that you can actually use if you just need the date or time you don't need the actual full date time format which is maybe something you actually need in your scripts the other very very useful option was get date and this is where it really comes into scripting especially if you're playing around with apis or different uh programs that maybe use UTC time or universal time now a lot of systems will actually use this because people will be using them from all different time zones and it is truly the only way to actually have a standardized date so oftentimes like Microsoft was the graph API will actually use the universal time now a way that you would actually be able to get the current date time as universal time as by doing get Dash date and then we have a parameter called as UTC now if we run this here we actually see that we get November 13th 2022 1107 49 pm and this is actually the universal time right now since I'm in Eastern time it is -5 which is why it is 607 right now here or 608 now um and if we do the as UTC we get 1108 to perform right now now you can actually do a lot of things with the asutc um parameter set you could actually do as well your custom formatting which we will see that in just a little bit here so if I just do uh something really quick here which I will explain how to do the custom formatting in just a little bit you will be able to get a custom formatting in UTC as well you can also do the display hint and just get the time in UTC if that's all you really need it as well now another option that you can actually do to get a time or a date time in UTC time is if you have a script here and you get the date in a variable what you can actually do is get that variable in a UTC type so if we just do the dollar sign date as get date and we display that date we of course just get today's date November 13th 609 now if we do the date dot to Universal time open and close parentheses that will actually convert that time in that variable to the universal time you can actually do this with all the get Dash date time so if you actually just do get Dash date in parentheses and then to Universal time opening close parentheses you will get the current time in Universal time and you can actually do this with all the different options here except the ones that return the strings um so even if you did the display hint here and time if you did this you will actually notice that we get the full date time back that will not work also if you did a custom formatting as well uh that will not work either because when you're doing a custom formatting you're actually returning a string and not a date time so that is something to consider but as long as you're using just the get Dash date uh commandlet you could do the taught dot universal time if you're doing like any other type of parameters I would stick to using the parameter as UTC time that will be your best bet but just know that the dot method of two universal time is there if you do want to manipulate date time variables now our next thing that we kind of hinted towards already is getting dates in custom formatting so this is very very useful especially if you're writing logs or writing to different applications and you need dates in specific formats so there's actually two types of custom formatting there is a u format and a format option we're going to cover the format option first and then we're going to cover the U format um I will be posting links to more information on these in the description below uh just because there are so many different options that you can do for formatting I will not be able to cover all of them I will cover the ones that I think are probably your most likely ones to be used more commonly uh and the ones that I use most commonly anyways but there are a lot of different options that you can do so let's start off with format here now format takes in a string and you can put in a bunch of different uh characters that will represent different items in the time so if you want to represent the year it is actually Four lowercase y's and then if you want to do the month you're going to want to do two uppercase M's you could also just do one uppercase M but two uppercase M's will go 0 1 through 12. if you only do one uppercase M it will do one through twelve a lot of applications that I have worked with in the past usually like that zero but if your application does not no worries you could just put one m and it will not add the zero in front same thing for the D's lowercase D's will be the date and that is the same thing if you only put one lowercase D that will do the number of days one group 31. if you do two lowercase D's that will do the date in 0 1 through 31. so that is again very useful if your application requires that zero in front of the one and then we could do a capital Double H for a 24 hour format at time lowercase M's for the amount of minutes with the zero in front of the single digit numbers a single M if you just want one through 59 double M if you want zero one through 59 and we even have you can specify if you want the date of the week the day of the week so like Sunday for Monday or Monday through Sunday uh you can actually do four lowercase D's so if we actually output this string here we will actually get 2022-11-13 1813 Sunday because today is a Sunday um and it is 6 13 it is November 13 2022 so that is something to definitely that you can use and you can add a whole bunch of different like you don't need to put colon here in between the time you could put a dash uh you cannot put dashes you can do it any way you really want and it will format it that way another thing that you can actually do as well is if you specify a k I believe yes okay uh K will give you the time difference from UTC time so here we have minus five so those are all things that are very very useful with the dash format the other formatting method here that we could use um if you wanted to keep the date in a date format so right now if I actually assign this to a variable as we've seen it with the dot method this date F will actually be so if I do a get type on this one here and open and close parentheses we will see that this is a string and if I do it on this date up here it is a date time so that is something to keep in mind there are ways that you could just use the dollar sign date from get date you can do date dot methods and you can get the hour the year of the month that way and you can write out your own strings as well but when you have this format option it is definitely a lot easier as an example if we did a write output here we would have to wrap all of them in variable wrappers and then do date dot year and then Dash and then another variable wrapper date dot month and then Dash and then another variable wrapper date dot day so as you can see like this could get quite long compared to just writing out a simple string here but it does work and it would keep your variable in a date to time format now the other formatting method is the get date Dash U format now this one is also a little bit this one's a little bit more confusing for me personally I usually use the just Dash format option but I'm sure if you get used to it it is quite easy to use um now here we would actually be able to use percent capital Y for a year Capital lowercase m per month and then Dash lowercase uh percent lowercase D for day and that would get us the year month and day now of course there are all again a lot of other options that you can use and as I said I'll be posting the links for the format and you format different character codes that you can put I'll be putting that in the description down below now on top of getting the current day and uh time you can actually create your own dates as well so if you needed to maybe compare today's date against the date in the future um or you wanted to do something with some type of custom date I'm not too too sure uh where you would use this other than in a comparison of of dates um but you can actually do like get date year where you can specify the year you can specify the month you can specify the day as well so here we're going to create a date December 2nd 2022 and you can even specify the hour so let's do the 13th Hour the fifth minute and the 30th second if we actually run this here we will get December 2nd 2022 105 30 p.m so this would be useful again like if you're running a script that's constantly running and there's like specific days that will trigger an email notification or something you can easily create a date in those scripts to check against to see if the current date is equal that date then do X action um and you can also even get the day of the year as well let's say maybe your application every time it reaches the 150th day of the year this needs to happen and it's not necessarily always um March 5th or something I don't know what would be the 150th day of the year but what you can actually do to get the day of the year is if you didn't get Dash date wrap it in parentheses and do a day of year you can actually get the current day of the year so right now being November 13th it is actually the 317 a day and you can do this on any get date option so even if we did it on our December uh second date we do day of year we can actually run this here and it will actually tell us December 2nd is the 336th day of the year so if we had something that needs to run on that day um that will actually tell us when to actually execute it or if we want to know how many days are in the year we could just always put like December 31st of that year and see if it's a year that has 365 days or a year that has 366 days that will actually tell us how that works now the last thing that we're actually going to be going over in this video is actually how to do a date like a time stamp option now this is good for writing logs or if your application exports files maybe you want to also time stamp those files so you know when that export was created now the way that I would personally do that there are a lot of different ways to do it um so there is a get date Dash format again what I would typically do is my four lowercase y's two uppercase M's after a dash and then another Dash lowercase D's so double m double d and then a t now the T will actually stand for nothing uh it'll just be added into the string I usually just use it to represent time and then I will do two Capital H's for the hours in 24 hour format two are low lower case M's after a dash and then Dash SS for seconds so if I actually run this here we get 2022-11-13 and then t for time 18 21.01 so I know it was at 6 21 on November 13th 2022 now the dashes are just what I pick but you can actually do Dots here as well or you can do underscores as well anything as long as it is a a valid formatting for file names like colons aren't valid uh characters for file names so that's not something that I would typically pick I like the dashes it's pretty easy to read very easy to kind of remember but that is how it is set so that artists all pretty much the different parameters that we really have with our get date there is one final thing that I do want to show uh which is the get date and then there is a parameter called Unix times second now I don't know if a lot of you guys will actually use this I haven't used it all of that often I think there are a few um fields and active directory that are actually stored in this time format uh so this is actually the number of seconds that have passed since I believe 1971 January 1st um so actually if we just do one here so get date unit time seconds one it will actually give up oh sorry 1969 so December 31st 1969 701 PM if we did two you're going to notice that it is just 702 PM so it is the number of seconds since December 31st 1969. um which actually in UTC time would actually be 1970 January 1st um so that is something to kind of take into consideration with your with your Regional time settings um but the Unix time is just a bunch of numbers uh that represents the seconds since that start of time there uh since the start of computer time and that is pretty much it for the different parameters with get date so if you guys have any comments or questions please let me know Down Below in the comment section I will try my best to answer all of them directly that's something that is a little bit more generic or general that a lot of people can benefit from I will make a video on it also be sure to hit that subscribe button hit that like button be sure to hit that notification Bell to be notified when that next video comes out I will see you guys on that next video
Info
Channel: JackedProgrammer
Views: 3,047
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, scripting, powershell scripting, powershell scripting tutorial, powershell tutorial, how to, powershell api responses, powershell automation, powershell beginners, powershell variables, quick tips powershell, quick tips, learn powershell, powershell for beginners, powershell commands for beginners, get-date, powershell get-date, date, powershell get-date utc, datetime, powershell dates, powershell datetime
Id: bCFvoSK5NVo
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Thu Nov 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.