Bad Date Comparisons in C# - if (DateTime.Now.ToString() == '01/01/2022 00:00:00')

preview_player
Показать описание
An overly detailed analysis of a bad DateTime comparison in C#

if (DateTime.Now.ToString() == "01/01/2022 00:00:00")
{
Console.WriteLine("Happy New Year!");
}
else
{
Console.WriteLine("It's still 2021…");
}
Рекомендации по теме
Комментарии
Автор

Great analysis, Scott! The first thing that I noticed when saw this originally was that even if they had gotten the DateTime string conversions correct, it would only be 2022 for 1 second. The comparison should have been >=, not ==. I honestly hadn't even considered your other bullet points because that one struck me so hard. LOL

jamienordmeyer
Автор

And this is why Scott is who he is. He took a thing most of us simply mocked and made fun of and turned it into a teachable moment. Kudos Scott.

_GrumpyOldPunk
Автор

Fun to see you "Exchange" information on how to parse dates correctly. :) The TIMING is perfect.

weisserth
Автор

"Virtually everything with dates is hard" - truer words have never been spoken :)

ociemitchell
Автор

I don't see another flaw mentioned - the else will display "it's still 2021" even when it's 2022 already. But yes, sad when marketing people take over, and thanks for the video!

ConstantinNovoselsky
Автор

I discovered your channel today and I'm so glad I did! You're voice is so relaxing and you explain everything in such detail. You're like the Bob Ross of coding.

Scifer
Автор

Great video Scott! There is another issue with the original string. You pointed out the leading zero day/month month/day issues, but the time component is also very different and incompatible. Because your culture settings are set to use a 12-hour clock, there is no such thing as 00:00:00 as it would be 12:00:00 AM in your case. The fixes you mentioned obviously still apply, but it's worth pointing out that your solution of instantiating a new date saves you having to deal with the time, avoiding the problem altogether. Always solve the smallest problem possible 😉

fabienruffin
Автор

Scott for what it's worth... I've been coding for 25 plus years and I don't know if anyone could have explained this better. Well done!

ADPrevost
Автор

Couple of other observations. Line 1 is trying to compare (with your culture setting) a 12 hour clock time and a 24 hour clock time. Also, unless you get lucky, the comparison might not happen at midnight exactly (strictly, with the format you have, between midnight and midnight plus one second) so you might never get the HNY message. The code could continue to output that it is 2021 whenever it is run. Despite it being 2022.

gavintillman
Автор

Always like hearing you talk about toy code examples. Thanks!

mattcargile
Автор

Hey Scott, great short video about common misuses in C#, i would appreciate more of that. We all got burned on such things as beginners. My favourite one is, vs . as decimal separator in different cultures :)

DivisPetr
Автор

Scott never disappoints. A long troll video with his soft voice. Liked that gif you shared on twitter for this tweet 👍

neyandeva
Автор

This was really helpful! Thanks! I'm an visual designer who's trying to start coding my own apps and this was the ureka moment for me to understand datetime! Thanks again cause I was really struggling to grasp how I should use it.

MykelGloober
Автор

This should be the first lesson of the first hour in any programming course!

LVertenten
Автор

Awesome as always .
You only get this kind of legend once .
I truly admire you Scott.

nikhilchakravarty
Автор

I can't believe you didn't mention the missing block of code 'You you = new You()' as having a new You is essential to any New Year function.... :) Love the content as always!

birdasaurusrex
Автор

Also, the string conversion adds AM/PM to the end, so wouldn't have worked even with the proper ddmmyyyy. Great video!

dave
Автор

I love your customized prompts, fonts, etc. However, it is sometimes difficult to determine if it is the font ligatures or not when something looks wrong. For example, your "==" turns into a single, longer equal sign. Maybe your font also converted the ellipsis into a special ligature. I wouldn't fault anyone for not picking that out in your screenshot.

strickdd
Автор

Thank you for a great explanation. Also, thanks for zooming in on your code, so it is easy to view.

PSeneca
Автор

Learned a heap from this analysis! Ty Scott

JesseLoudon