DateTime Deep Dive in C# plus DateOnly and TimeOnly in .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the date time keyword in c sharp is commonly used but have you ever really explored all it can do in this video we're looking at the date time object what it represents identifying how to use it to get more relevant data out manipulating it displaying in various formats and a lot more we'll also be looking at how the date only and time only types in.net 6 will help supplement what date time can do now this is the first time you've watched a video of mine my name is tim corey and it's my goal to make learning c-sharp easier i provide videos here at least twice a week on youtube to help you grow as a developer i also have a full set of training courses on iamtimcory.com i encourage you to check out all the resources that i have to offer let's go over to visual studio and just so you know i do provide a source code for this video if you want to have my copy of the source code i'll do everything on camera but if you want the source code it is down the description but we're going to use visual studio 2022 this is a preview version because it's not quite out yet but it is a version we can use in production so let's create a new project and we'll create a console application we're just going to play around with things today we're not necessarily going to be creating a working application that does something cool other than just show off date time date time info let's call it that and date time info app and we use net 6 which is the long term supported version of.net so that's the latest version and yes it is ready to go live even though it hasn't officially launched yet this is the go live version so let's hit create and with that we now have our console app now if you're not familiar the console app has changed a little bit because of the fact that we have implicit usings and we have global usings and we have the top level namespaces and a whole bunch of other stuff that comes with net six and it kind of all accumulates on the program.cs so everything here is you could say it's hidden but it's not really hidden what it is is it's implied so because we're in program.cs this is where the application starts so instead of having our our namespace which would be just datetimeinfo and instead of saying static void main which is a thing we have to do in every program.cs we just have the code section so you can put code in here and this will be inside the static void main method so just you know that's what's going on now let's look at date time so we're going to do is say date time and then we'll say um let's just call it sample one okay equals new date time that's our our date time now date time is a read-only struct so what happens there is kind of a class and we'll cover structures more um in the in the following videos but what it is that has a whole bunch of properties and methods around our data so let's first start by just doing a console write line sample one okay just print out whatever is in sample one right now which is a new date time let's do that and when we do we'll see that we get this date time right here which is january 1st of the year one at midnight so that is the default value for date time if we don't give it anything so that's not terribly useful but it's good to know where it starts okay so let's talk about how you could use this so instead of new date time we can say instead is start with a known value so datetime.now and what date time dot now is is it will be the time right now so right now it is october 22nd and it is 6 44 p.m so that's the time right now and so we can then use that but we're going to talk through some other things first before we get into how to print out various things so let's talk about a few more of these properties so now he is going to get the time based upon local time so i'm in a central time zone so right now my time is 6 44 p.m central time however if we were to want to know about let's say eastern time we'll have to do some manipulation on that or we can do is use instead of now grab utc now utc for universal coordinated universal time which yes utc coordinated universal time it switches the acronym around but um that is essentially the uh time zero where there's no offset okay that there is no offset and so right now my offset i believe is six hours um so therefore oh it's five hours right now because of daylight savings time time and date or especially time is tricky so you're gonna have to figure out a lot of these things and i'll talk you through some ways to help with this there are other libraries can help um we'll talk more about noaa time in the future but um noah time is probably one of the best libraries out there to make it a lot easier to work with date and time in c sharp but for the actual date time object in c sharp just note that this is now utc time which then i could convert into any time i want this is very important because if you're capturing logs and you capture the local time and one server is on the east coast and one service on the west coast well then the west coast server of the us then the west coast server will be logging times three hours earlier than the east coast server even though they're happening at the exact same time that could be a problem okay it's also a problem if you say well we're only gonna have our servers in central cool we're good yeah but what happens we have a disaster and you have to fall over to the west coast backup site well now all your times are two hours difference compared to the central time and that could be an issue so by using utc time that you know that the time is the same for everybody okay so this would print the same time if you're on the east coast or west coast if you use utc now what you do lose though is you do lose that time zone information you don't know which zone this was captured in and that can be relevant okay so just note that you're actually losing information when you say utc now versus just now however you are synchronizing your times in a way that's a little more easy because the fact they're always the same time relative to each other okay so there's now and utc now all right and then we also have the ability to say min value which we've already seen them in value that is going to be that january 1st year 1 at midnight but there's also a max value this is the largest date time value you can have i guess after this we just can't have c sharp anymore kind of like the y2k issues of 20 years ago well now in the year 9999 when we hit january 1st the next year we're going to blow up because that's as far as this goes okay so that's the max value you can have for a a time a date time now it is important to note that date time does store everything we've seen this let's go back to let's go to now and you'll see that it stores the date and the time so date time stores everything all right now you can make this nullable as well so if you wanted to you could say question mark in front or after date time and then now we can store a null value in here this can be really helpful for when you are not sure if it's a the starter date the min date or whether it is um a null value so let's say they haven't given you a date time yet well you store null in there that's what databases are used to as well but then you have a value you'd have to figure out is this the min value is this the max value you know a value that you'd know isn't a real date instead usa no problems null that's not been filled if it has data in there then it's a real date time so that can be helpful as well there's also the option let's take this off from nullable there is also the option of datetime.today okay so that's going to get today as an object but the time component will be 0. so if you run this note that it is midnight now there's still a time associated and we're going to see the date only in the future and in this video but that's not the same thing because this is just today which is just represented date but the time is set to zero so that is available to you um there's also this this idea of the oops sorry let's go today and then down here we can say ticks this represents the ticks of the date time of this instance so a tick is 1 10 millionth of a second okay and it's going to count up notice the very large number right there it's going to count up from january 1st of the year 1 at midnight it's going to say how many ticks there have been since then so lots all right um so that's min value.tx and max layout text as well so that'll tell you so if we did this um min value you'll see that the ticks right here are zero okay because that's the um the minimum value which is that january 1st year one well the ticks then are zero so if we had um you know a day after that you'd see just the ticks for that one day but that is the ticks that's what they're there for that will give you again a set value to work with here's how many ticks have been since this point in time all right let's talk about some properties on the date time value so one is really useful is day of the week okay so day of the week what is that it's friday okay so it tells you which day it is and of course that's a day of the week object which is going to have one of seven values um sunday through saturday all right so that's the day of the week we also have the option of day of the year so if you want to know what day is it in the year was the 295th day of the year getting pretty close to the end of the year so that can be helpful as well um we also have the option here of is daylight savings time and this will give you a boolean so it's going to indicate oops this is a method sorry there we go it's going to indicate whether this instance of date time is within the daylight savings time range the current time zone so this is going to tell you are you in daylight savings time right now the answer is true so that will change once daylight savings time turns off and again date times really tricky to work with because of daylight savings time it's trying to figure out notice the time range for the current time zone so if the time zone does not respect daily savings time then it will save false even when in the time zone next to it does respect the time zone or data center time and therefore it's a true so that can be tricky but that will allow you to figure out whether or not you're in daylight savings time right now next up we have kind so let's do this let's um put kind on we're going to create a second sample we'll do sample 2 and this will be utc now and then we'll do sample 2 kind as well makes it a little quicker okay so now the first one is based upon local time the second one is based upon that coordinated universal time so this allows us to know hey what's this value is it is a local time meaning has a time zone associated or is this utc now there can also be a kind of unknown if it does not know whether it is local or utc that can be a very bad place to be because if you have a date and time but do not know what time zone is associated with then you've got a problem because you don't know what that represents really it's a nebulous value so you want to know what kind it is okay with that let's talk about uh ways to manipulate the date time so this date time dot now is the local time so two we could say to universal time so this will convert that to universal time we could do the same thing down here with our utc time to oops not kind to local time like so and convert the other direction so i've just flip-flopped these so now the top one is the utc time the bottom one is the local time even though that's the reverse of what they started from okay so by saying to utc or to local i'm able to change those values out again if i know what kind it is okay so now let's do this let's comment these out and we're going to work with sample one dot add okay and i can add a number of different things here so let's talk about the different things we can add because it's going to introduce a new variable type so that variable type is the time span we're going to skip over this and come back to this because i think it's it's simpler to see first of all the um the other ones but they'll come back to time span talk about it so we have add hours so we can take our current time and add some time to it add an hour to it and make it one hour later or and so on we can do the same thing with days hours milliseconds minutes basically anything we can even add ticks okay so we can add quite a few different things but let's add let's add hours and we're going to say i'm going to add three hours it's my time and that is my date time.now so let's say one and i can just say sample one it's technically doing a dot two string but um we're gonna say sample one so now if i run this we'll see that the time says oops i said sample one dot add hours but this returns a date time i'm gonna say like that that will overwrite sample one or i could say let's just do it right down in line like so okay just know it's not gonna modify the original it's going to create a new one so now let's do this and now we have 9 58 p.m even though it's currently 6 58 p.m so we can add hours days minutes and so on to our time and you may say well tim but how do you subtract because i don't see a subtract method in the list that's because we can add a negative value okay in fact let's do this let's let's actually print out the original so sample one and then we'll print out this one with the add this one with the subtract so i remove three hours from the time and now we have the original which is 659 the new one which is added three hours 959 and the one that has subtracted three hours and we're at 359. so that's how we can manipulate our date time we can do that for days hours minutes doesn't really matter also note if we assign to change this notice 30 this will figure out what that means so it's now just after midnight on the 24th and it's just it's almost one o'clock in the afternoon on the 21st okay today's the 22nd so this is because the fact we've changed at 30 hours it rolled over our date two it knows about the fact that 24 hours is one day and it will respect things like the start of the year and so on so that's how we can use add now let's talk about the time span okay this is a type that is often confused as to what it represents a time span does not represent a single point in time instead a time span represents a a span of time meaning how many hours minutes seconds and so on so so time span we could do from seconds from milliseconds we can do parse we could do from days hours and so on um so we can figure out we can create a time span where we parse out and say okay i want to create a time span notice there's three different overloads we're going to use the first one there we go uh the first one which is just a string and this string will represent our uh our time span so this is going to be our hours let's say it's 4 hours 15 minutes 13 seconds and we can have milliseconds as well so let's say um one two three four five so this is going to be um how many fractions of a second so this is uh 0.12345 out of 10 000. so that's our representation of the milliseconds so hours minutes seconds notice the dot here and then milliseconds this will create a time span for us and if we just did uh console right line of time span and run this we'll see that there is our time span represented notice you can go even more fractionally with your milliseconds if you want to but we can now use this time span to do things like add to our time so console write line sample one dot add notice the time span value we can add that time span to it so our current time add four hours 15 minutes 13 seconds and the fractional as well so there is the time right now there is the time that it would be if we add four hours that's from 7 to 11 15 minutes 15 minutes yep 15 minutes which is 4 plus 15 is 19 13 seconds so 11 plus 13 is 24. notice the fractional is not being shown that's because the type of how we're printing out this string okay we're going to deal with how to print out more information from our date time so that time span represents the point between two times okay it does not represent four in the afternoon it does not represent four in the morning it represents the difference between two times that's why it's called a time span all right so be careful there this is not time only which is the new keyword in c sharp this is the difference between two points okay so this gives us our duration of the difference okay so we can now do things like we can parse it out oh by the way we can also add days here if we wanted to um if we said five there we go so five days puts us at the 27th at 11 00 20 p.m so just so you know we can add that additional day i think day is as high as it goes but that is possible to add days to it so we can use time span we can also get time span so if we were to um grab a time and then let's just take here we go let's just take our our sample 1 where you've added in the time span let's start there and we're going to say [Music] sample 1 dot add to ys so we know what that value is okay we know that value is whatever the local time is right now added 5 days 4 hours 15 minutes 13 seconds and so on so we can now that's now a new date time so we can say date time sample three equals sample one dot add okay now we have that we can do is we can subtract one date from another so we can say sample three dot subtract and notice we have two options here we can subtract another date time value or we can subtract a time span let's look at the date time value that's what we're going to do first so if we subtract the date time value then we get a time span in return so if we have two dates and we say subtract one from the other then we get a time span because that's the difference between those two points in time now remember it's the full date and time so it's accounting for everything there so if you were anticipating how many days between the two you gotta be careful because if the times are off then you may have one less or one more date or one more day so um that's be careful of that's why we use things like today which is midnight and so on but with this we can subtract let's say sample one and that will give us a new time span now we can grab this time span um but let's just wrap it in a console right line since we have it and this is going to be our difference between these two dates now we know what the difference is it has this time span right here but we've done the addition got a new date and then compared this date with this date to get a time span out let's just see what that time span is right there that's the time span notice it exactly matches this time span up here because the fact that we have added in and subtracted those two dates and found the difference we could also do something else where we say console writeline sample let's do sample one dot add and add time stamp i'm sorry add a uh yeah time span or we can say is sample three dot subtract and also subtract the same time span so we've added time span here we can subtract the time span back out and get the original date as well so again um that's the original date and time that's the date and time after you've added the time span and subtracted it back out so yes that's kind of simple but just while you know you can do that you can subtract or you can add time spans so that allows us to play around with the differences between two dates but here's where it gets even better maybe you say you know what we've got this sample we've got this this date right here okay sample one and that's today's date and time and we know that the difference between the two dates is this long but we want to hit the middle how you figure out where the middle point is between the the date and time it is right now and the end result so we've done subtraction we figure out this is the the difference between two dates we want the halfway point not a problem we can do is come down here and say this is going to get a little confusing so let's do some string interpolation here um halfway point and then we'll do our actual work here so we're gonna say that it's uh sample one add we need a time span so ts which is our time span dot divide by two so we're dividing our time span by two remember the time span is a difference between two dates so it's five days four hours and so on so it's now two and a half days two hours uh 7.5 minutes and so on okay so we're gonna add that to our sample one and get the halfway point and that is our halfway point so here is our original time here is the halfway point when we add two and a half days to it so 22 at seven in night we've now got 25 at nine in the morning so that's our two and a half days from when we're working with we can do the same thing not just divide we can also multiply so if you want to say well that's the halfway point we want to multiply or go twice as far so we can say multiply by two or by whatever um let's go triple so multiply by three just to know that yes we can do this we're gonna multiply by three and that's now our triple point which is november 7th at almost eight in the morning so this allows you to play around with these and figure out the differences between dates multiply divide and so on the time span has a lot of cool stuff this is not a time span video but i do want to point out a couple of things in here that you should know about so as some really cool stuff in here for example besides multiply and divide we have totals and i do want to point this out because this will get confusing okay so let's do this let's i'm going to comment these out i'll leave them here but i'm gonna comment them out because it's getting a little overwhelming let's comment all that out and we're gonna do we'll leave that one in sample three okay so down here we're going to start working with the time span and we're going to say you know what i want to see and a lot of cool stuff in here let's see the number of days how many days are in here all right so we run this and we get the number five well that makes sense because there's five days in here let's talk about number of minutes okay we get in there we say 15. you're like um is it though because yes it says 15 here and that's what we used but i think that there's more than 15 minutes in this total time span and that's because this is minutes not total minutes okay there's also the concept of total on all of our different values so if we run this again we'll see the total number of minutes is 7455 and a fractional so that's the total number of minutes in that time span so when you're working with these values make sure that you understand the values you're getting is it total milliseconds or just a milliseconds the the whole number of that that fractional piece so the same thing with um a number of different things there's minutes there is seconds there's days there's ticks but then down here is where you probably want to live and that's total days total hours total milliseconds total minutes seconds so that's where you find the um the totals if that's what you're looking for so if you're saying how many minutes between these two dates you don't want minutes you want total minutes okay so just be aware of that difference because even if it was let's say 15 minutes you would get the number 15 not 15.6 because it's really 15.6 minutes between the two dates but we're only taking the the minutes themselves um the minute portion of the time span so just note that's the difference there okay and we also have this idea of duration so the duration is important and the reason why and if we look at this you're gonna say what's the difference here because it's still 5.0415 yadda yadda okay well what if this was negative okay so let's just print out sample three we've added the time span okay so let's just print sample three um so we have that sample three to know that yes we're going back in time okay so we've gone back in time to the 17th of october when today is the 22nd of october however notice that the duration is still this value the 5 days 4 hours 15 minutes 13 seconds and so on the reason why is because the duration says it doesn't matter if you're going one way or the other if you just want the absolute value the difference between the two not worrying about negative versus positive okay otherwise you have to make sure that your second date is the one that you use to then subtract the other date from otherwise uh what happens to get a negative value so this way by saying duration of our time stamp our time span that gives us the absolute value that we can then say it's five hours between the two of them or five days between the two of them okay so that's that's time span yes this is a date time video and i'm not gonna go into uh every type surrounding the date time but um i thought that's important because the fact we can do things like date manipulation where we subtract one date from another like this okay notice i did the bigger date subtracting a smaller date that gives us a positive value we could flip this around sample one dot subtract sample three that would give us a negative value and then we'd have to do dot duration to get the actual the absolute value the non-signed version of that okay so that gives us some differences here now let's start working with how to compare dates okay so we have uh date one sample one is our our now sample two is our utc now and sample three is our uh are you are now with five days subtracted from it okay so now we can do it we can do some comparisons okay so you can say date not date only date time dot compare we can compare two different values so let's compare sample one and sample three all right so compare these two values and get back a response and let's um let's actually do a console right line just wrap this thing in and we'll say comparing sample 1 to sample three oops not capital s and then we'll do the actual comparison right there okay so with that we will see that the comparison it says one all right so so what does that mean okay um that means that sample three is greater than i'm sorry sample one is greater than sample three okay um yes they can get confusing so let's do this let's copy this a couple more times actually i can unpin this to get a little more space and we'll compare the opposite direction okay so make sure you modify this and then we'll do it one more time and this timer compares sample one to sample one so comparing sample 1 to sample 3 says that sample 1 is greater than sample 1 is this one right here sample 3 is this one right here sample 3 is a smaller one so it says sample one is bigger because it's positive one sample three is smaller so it's a negative one and then sample one is the same as sample one therefore zero this allows you to compare two dates and just figure out which one comes first you can do that with the one negative one and zero know these numbers will not change based upon how great the difference is it's just going to be um if it's bigger or smaller that's all it cares about okay and that does depend on the order you put things in so if it is greater if the first one is greater it's positive one if the second one is greater it's negative one and if they're equal it's zero all right so that's the the difference there so there is our comparison and you've worked with time span as well but now let's let's comment that out and let's talk about parsing there is so much stuff we can do with parsing we're gonna parse on sample one in fact i'm gonna comment out um not the declaration but pretty much everything else just to get um this stuff off our console um so we have just whoops i can't parse out the declaration of time span there we go um so with that we now have an empty console the console's not printing anything yet we just have our sample one two our time span and our sample three so let's talk about how to print out so far what we've been doing is saying sample one okay which technically is two string that's what happens inside right line is it does a two string and anything you pass into it so therefore we get a value like this now we can manipulate this by passing in what we want to in the tostring using double quotes the cool thing is this editor now has get off my screen it now has the ability to show us what our options are there's a lot of options in here so let's talk about some of the more common ones okay i'm actually going to print out a number of these so what we'll do is um we'll actually do uh this we're going to cut this out and we'll do a string interpolation so you have kind of notes on this short date and then we'll do small d and then we'll have one more it must be a long date known as a capital d and then we can do a few more let's do times as well so i'll copy this and paste and again the casing matters here so short time is a small t and long time is the upper case t let's see is an action so remember it's the same date okay but the short date is just the date portion the long date includes the day the printed out month and so on okay you know it's nice formatting short time is just 7 25 p.m long time includes the seconds that is printed out but i want to be clear here just because we're printing out just the date does not mean that's all the object holds this same object is printing out four different ways we can print out even more ways as well so that's different ways to quickly just format a date for your screen let's do a couple more let's copy this and we're going to i'll put a space here to separate them out and let's look at i believe it's o yep round trip date time and round trip date time like so and we'll see that we do this that gives us a lot of information it says 2021-10-22 that's our date portion then it says t for time it gives it to us in what in the u.s called military time which is the 24-hour time span instead of 12 that way we can see without having to have the am and pm what the number represents so 19 26 09 and then the this is the milliseconds there so there's our hours minutes seconds and milliseconds and then we have negative five this tells us our time zone offset all right this does not tell you the time zone it just tells you the offset for the time zone that's kind of important because there are multiple time zones that have the same offset at any given period time yes again that can be confusing but what this does is it tells me more information than just utc remember ut sees what we had with uh sample two and that would be just zeros here but with this it tells me okay we are at the negative five hour offset so that i could recreate the date and time locally or at utc using this information and that's what this round trip date time does is it gives us all the information so you can basically make a choice if we want to see it later in utc or if we want to see it in local time for really any given local time that you want so that is the round trip all right and then we have there's a whole bunch more let's go over um go over these so let's start with let's start with f which is the full short okay [Music] so this is the full short date time and then there is the full long date time and yes it's important to know these or at least know how to get to them or that they're available because i often see people trying to recreate these with custom formats when they're just there for you as a single letter so um then we can do general short date time and general long date time and that would be the g's all right so let's look at these so we go um we've got our full short date time which is friday october 22nd at 7 29 pm notice that the difference between this and the long is this includes the seconds where this one does not this is the general that's the full though which means you get the full date but the short time this you get the full date and a long time this is the general date short time general date long time okay so there's the differences there but again i usually find that i'm printing stuff like this so i just capital g you're good to go all right so remember g for good that's all you need now there's also sortable times okay so let's bring these in we can get the the lowercase s for sortabledatetime and we also have the uppercase u for universal all right so universal and then we have the sortable and again we can run this and see that the sortable allows us to basically print this in a list and then sort by the characters and it will properly sort through because it um it knows all the information it needs without the offset but knows the information it needs to be stored in a list easily whereas this one right here universal gives us the universal time right now which is utc so it's it's grabbed the offset and said let's back that up so you're negative five so add five on and here is the new time it's actually saturday at this point at 12 30 in the morning so again some cool stuff there but probably you're gonna look at all this stuff and go i want one little tweak okay yeah it happens it's unfortunate um i personally recommend the capital g but if you say you know i really need to have my own stuff no worries you can do that so let's grab let's grab one of them i'll copy it i'll paste it and we'll say custom so instead of having just the one letter you can customize how you want to look for example you could say something like mmm for the month now capital no it's a capital versus lowercase that is important and the example here shows you that this would be j-u-n for june all right so whereas the capital one more m would print out all of june two m's would print out o six and one would print out well that's the um the month and day but if you did this one it's going to say six so this kind of points out something that if you use one letter then you're probably using one of these shortcuts we've already done but if you use multiple in the string it's going to use those same letters but because there's multiple it's going to say oh you don't mean the the single letter formatter you mean the letter that represents a certain piece of the date time yes that's confusing so we reuse capital m either as month day so june 15 which i didn't show you up here but you could do that or we use it as the month character one or two digits in our date so i'm going to use mmm for month month month and then we need day so i'll say dd for day of the month two digits so if it is less than 10 it's going to say like 09 or 08 as opposed to just 9 or 8. so dd and i can put a comma and i can put y y yy i can have a four digit year notice there is a five digit year you could have if you really wanted to have that leading zero so and then we can do is we can say from here i can say i want let's go with two hours so that's gonna have 07 because it's 7 o'clock at night but notice it's lowercase h but if i use uppercase h i could get the 24 hour clock and it would say what was it it's 7 o'clock at night so 7 plus 12 is 19 instead of 07. so we could choose that if we want to i'm going to use the 12 hour clock i'll hit colon and because colon is not a a formatting keyword or key letter then i can just it'll just print out that colon so colon and then i can say i want minutes but again get this right lowercase 2n because the uppercase ms are months okay i don't know how many times i have put the wrong uppercase versus lowercase and wonder why things look wacky so and then i can say you know what i don't want seconds and then i want um let's do let's do the t uh t t for am pm okay that'll show me if i'm in the am or pm and then i do want the time zone offset okay so it's going to give me the um the ability to see the time zone i'm sorry not k it would just k if i wanted to have that i'm going to do z z z okay that's the hour and minute offset for my time zap time zone so with that if i run this i will see that i oct oct 22 20 21 7 35 p.m negative five that's my offset all right so that gives you a lot more information about how your time is laid out you could even say you know what i want more information and you could get additional information or you could change up how it lays out in sohan so fourth for instance if you're in europe you may want to see let's just copy and paste this um you may wanna see the month i'm sorry the day first and then the month like so um and actually let's just put the slashes in instead and then we'll do a two digit year and then we'll do the um 24 hour clock which means we don't need it am pm like so and now we take that same object in format 22 36 36 doesn't doesn't seem right um let's figure out what i did wrong there ah told you i'd mess it up there we go capital m versus lowercase m um whenever your month says 36 you got a problem so 22nd of october 2021 at 1936 with a negative five offset on our our time so that gives you the ability to format your date and time however you want and that that um that menu oops i clicked off of here i'll close it out this menu here will give you a lot of great information about uh control space brings it back up by the way you can scroll through here and say okay what am i looking for here i want the rfc 1123 date time well that's just r not r in the middle of all this but just r but then you can have every four matters hey you want the you know one millionth of a second well that's uh looks like six f's okay or 10 millionths of a second that's looks like seven f's i think if i can count the f correctly um so there's a lot of stuff in here you can play around with try out and get the date and time just how you want it however i would encourage you if one of these works for you then use one of those because the fact that by using one of those you have a standard layout okay you don't have something that's a little custom is a little bit different and might not quite look right all right now let's again comment these out and we're going to do a couple other things let's say date time sample 4. i like how it actually hey look at that it kind of assumed some stuff there but i want to say datetime.parse okay we can parse in based upon where i am from so i'm in my region is the u.s therefore it's the month day year so i can say 2021 that's march 11 2021 but i could also do date time sample five equals date time dot parse exact and i can say that same string all right but this time i can say here's the format to give it day month year and we're going to give it the culture info control dot to add that using for system.globalization cultureinfo.invariantculture okay what that means is we're not carrying with the culture we're just going to force it through in whatever format we decide is best this is really helpful if you're bringing things in from a csv file where people didn't listen to advice and do stuff like this they did stuff like this where it's just a little off and the parse doesn't pick it up properly no worries you can do parse exact and switch it around notice i've done day first which means this is the day this is the month so this is november 3rd instead of march 11th and let's just print out each of these um so let's just print out sample four and sample five yes i'm not labeling them but i think you can figure it out um but notice that march 11th versus november third because the fact that when we parsed this one it based upon my current culture my current culture says that month comes first then day then year whereas with parse exact i overrode the culture and said here's the format i want and that's the data coming in so if you're bringing data from a csv you would have the variable right here it would bring it in from the csv loop through it and you can convert it to a date time that you could then print off in any format you wanted to because now you have it in a full date time object okay so that's how you would parse out your date and time now there are some new changes i've already shared in a 10 minute training video but i wanted to show you here as well there are some new changes to net 6 and c 10 that make things a little bit easier for us so we have two new types date only let's call as just date or date one and then we'll also have time only time one all right now as i've talked about date only and time only are not just portions of date time they're a separate structure okay they're not like date time because the fact that they only only stores if you can you might guess it only stores dates it does not store anything about time and time only only stores time it does not store anything about about date so this can be very very useful when you're saying things like the store opens at eight in the morning and it closes at nine at night you're not talking about on a specific date you're not talking about based upon which time zone you're in you're talking about a specific time okay as a set of times and know what time span wouldn't work there because a time span is the difference between two times it's not two times okay so if you want open time you'd have this as an open time you have a separate time only as a close time so same thing with your your date here that could be your birthday because if you store your birthday at a date time and you know of course midnight well what happens if someone interprets that from a different time zone they could actually see your birthday as being in a different date because the fact the time zone offset might move it to the previous date or the yeah previous date so that would be problematic so this way we just store a date or not worrying about time zones my birthday is the same day every year regardless of what time zone i'm in okay so yes technically i was born in a specific time of day i believe it was actually like two in the morning but uh poor mom but this represents just the date itself okay so with date only and time only we can't just say equals date time dot now but we can say date only dot um from date time and we give it a date time dot now now i'm hoping in future versions we can say date only from today or something like that but this gives us then the date only we do the same exact thing with our time only like so um whoops we should probably get the time only portion of that but now we can say console write line date one and then console write line time one and if we run this we'll see that the date only is 10 22 and the time only is 7 44. we have a lot of cool options on the date only and time only so if we see date one dot we have a day we have two date time so we can take this and a time only and make a date time um we also have the month we have the day we have the year we can add days add months add years we can compare to something else and again that will give us an integer that compares greater than less than or equal to we have the day number the day of the week the day of the year just like in in our date time and then we can print out different strings or format it however we want using the same formatting tools but again we don't get those those formatting tools for things like time on date only okay and the same is true for time as well so if i said time one you're not going to see anything about the date in here but you'll see hours minutes seconds and so on that you can compare between again you can play a lot of these different things but they're basically the the time portions of what we had with date time but just the time and we have the date portions in date only but just the date so this really separates out date time into two distinct values if you need them okay you would not just have two different values to hold date and time and try and keep them coordinated you'd probably have just a date only or just a time only depending on the need if you need both then date time is the way to go as we've seen up here lots of power to it lots of stuff you can do with it lots of ways you can manipulate and play around with it but these will elevate your game when you're doing specific things that do not represent a specific date and time but a general date or a general time okay now we have not covered the date time offset which date time offset essentia does that it captures more information about the time zone so that we could have both the local time zone and utc and there can be some value in that but primarily in c sharp we work with date time so that's what i covered and we're already uh well into the the length that i wanted to get to this video i want to go beyond this so we haven't covered date time offset i may cover that in a future video let me know down the comments if you want to see a specific date time offset video and some of the stuff you can do with that object as well but this has been date time in a little bit of time span too because time span it just kind of fits right in with date time but date time can do a ton of cool stuff but now with date and time we also have some flexibility to not have to kind of manipulate the date just a date time to say well yeah but at midnight um or the time to just you know it's year one january 1st but ignore that okay we don't have to do that anymore but whenever you talk about date time you want both of those pieces then you want the date time type um if you just want a date or just want time those are now available in net six which is in visual studio 2022 and is being released in november of the year 2021 so um that's you know all the cool stuff you can do in c-sharp yes there's some complexities here and yes there's ways that they could make it a whole lot better but there is a third party library out there that if you are interested in doing more with date time that i encourage you at least look at if you're struggling with the built-in date time stuff and that is nodatime n-o-d-a-t-i-m-e and that's um that's a really great library that does some really cool stuff with date time it really helps you work with it well and work with the different time zones and so on so again that's kind of outside the scope of this video if you want to see that featured in a video at some point leave that message down in the comment and i will kind of keep track of of who's looking for that okay so thanks for watching thanks for going this deep dive here hopefully it was helpful i love what your favorite part of date time is or what your thoughts are on the new date and time objects in.net 6. thanks for watching as always i am tim cory [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 8,343
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, course, training, how to, tim corey, C# training, C# tutorial, .net core, datetime, timeonly, dateonly, c# date time, c# datetime format, .net 6, visual studio 2022
Id: e9HUcuQkbBU
Channel Id: undefined
Length: 66min 57sec (4017 seconds)
Published: Mon Nov 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.