DateTime.Now: Usage, Examples, Best Practices, and Pitfalls
For many developers, their first experience handling time in C# is by using DateTime.Now.
When needing to retrieve the current date and time, they’d search for it, reaching a StackOverflow question or some documentation page by Microsoft. They’d quickly find that the way you get the current instant is by using the “DateTime.Now.” They’d go back to their code, use the property, and be done with it.
Also, for many developers, the scenario described above is the extent of their education about DateTime.Now. This isn’t good. Why?
Well, time is one of the trickiest topics in programming. Not knowing about its intricacies can get you into trouble. We’re here to help avoid that, by taking a look at the dos and don’ts of DateTime.Now.
We’re going to cover what DateTime.Now is the basics of its use. Then we’ll go a bit deeper, showing some more advanced usage scenarios (i.e. output formatting), covering the best practices you should employ and also the pitfalls you must be aware of when working with this property.
Let’s get started.