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.


Welcome back to the CodeIt.Right Rules Explained series. For those of you who haven’t seen an installment in this series, let’s explain what it’s all about.?
One of the most anticipated acts at a circus is the juggler?a performer who can move five or six or more balls in the air at the same time. The really complicated juggling acts, however, add something extra to wow the crowd. The juggler?first climbs some stairs, high up but still close enough for everyone to see. He’s on a platform. The overhead lights are dim while one bright spotlight is on the juggler as he starts to juggle?one, two, three, four, five, and six balls moving effortlessly through the air, all in balance. He’s good!
The C# interface isn’t exactly intuitive.
Imagine you’re a software developer working on a financial application. One day you’re assigned (or, if you’re agile, you pick yourself) to implement changes to the overtime computation logic so your company can sell the application in France. In France, the official work week has 35 hours (lucky them) instead of the 40 hours in the US. Being new to the team, you’re not sure where to begin, so you start searching the codebase for the keyword “overtime.”
Rent, employment, house buying: you name it. If you’re an adult, chances are that you’ve signed several contracts in your life (and will probably sign many more). A contract is an agreement that defines the rights and obligations of two or more parties in order to accomplish something.
