<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl" -->

---
title: You should stop using DateTime.Now in your code! | daily.dev
description: Treating the current system time as an implicit, always-available value in production code creates hidden dependencies that make business logic harder to trust...
canonical: https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: You should stop using DateTime.Now in your code! | daily.dev
og:description: Treating the current system time as an implicit, always-available value in production code creates hidden dependencies that make business logic harder to trust...
og:url: https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl
og:image: https://api.daily.dev/og/posts/HtZDmhcHl.png
og:image:alt: You should stop using DateTime.Now in your code!
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# You should stop using DateTime.Now in your code!

**[UsmanCode](https://daily.dev/sources/usmancode)** · [@usmancode](https://daily.dev/usmancode) · 1 min read · 3 upvotes · 1 comments

## Summary

Treating the current system time as an implicit, always-available value in production code creates hidden dependencies that make business logic harder to trust and tests brittle. The recommended approach is to abstract time access (e.g., through an injectable clock/service) rather than calling DateTime.Now directly, so tests can control and predict time-dependent behavior.

## Content

The current time looks like a value you can grab whenever you need it. In production code, it is really an external dependency—and treating it that way makes your business rules easier to trust and your tests far less fragile, how I have explained here [https://www.usmancode.com/blog/you-should-stop-using-datetimenow-in-your-code](https://www.usmancode.com/blog/you-should-stop-using-datetimenow-in-your-code)

## Community discussion

Top comments from developers on daily.dev.

**@neilhatly** · 0 upvotes

> I'd add that any fixed system resource should be accessed by an interface, be it date time, file and folder access, Most of System.Runtime and even System.Random. Mocking those connections after the fact will be a rats nest of reflection and dark magiks

## Similar posts on daily.dev

- [Practical Dependency Injection in Python](https://daily.dev/posts/practical-dependency-injection-in-python-fy9wnnthl) · Lobsters · 2 upvotes · 0 comments
- [Stop Pretending TimeProvider Doesn't Exist — Daily DevOps & .NET](https://daily.dev/posts/stop-pretending-timeprovider-doesn-t-exist-daily-devops-net-8lez0ga1i) · We Are .NET · 21 upvotes · 1 comments
- [.NET TimeProvider: Test Tomorrow Without Waiting](https://daily.dev/posts/net-timeprovider-test-tomorrow-without-waiting-yn2fpuocl) · Medium · 0 upvotes · 0 comments
- [TimeProvider and FakeTimeProvider in .NET Tests](https://daily.dev/posts/timeprovider-and-faketimeprovider-in-net-tests-cpo0lbau7) · Milan Jovanović · 22 upvotes · 1 comments
- [Time in C\+\+: Once More About Testing](https://daily.dev/posts/time-in-c-once-more-about-testing-wm0vdb66u) · Sandor Dargo · 0 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#c#](https://daily.dev/tags/c#), [#dependency-injection](https://daily.dev/tags/dependency-injection)

[View this post on daily.dev](https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl","headline":"You should stop using DateTime.Now in your code!","text":"Treating the current system time as an implicit, always-available value in production code creates hidden dependencies that make business logic harder to trust and tests brittle. The recommended approach is to abstract time access (e.g., through an injectable clock/service) rather than calling DateTime.Now directly, so tests can control and predict time-dependent behavior.","url":"https://daily.dev/posts/you-should-stop-using-datetime-now-in-your-code--htzdmhchl","datePublished":"2026-09-04T04:40:37.123Z","dateModified":"2026-09-13T21:07:14.956Z","author":{"@type":"Person","name":"Usman Code","url":"https://daily.dev/usmancode","image":"https://lh3.googleusercontent.com/a/ACg8ocJhPsgkzjjJEwSKAfcFhWNpsOuhTroZor5spSeio1E_db3B_GAbzQ=s96-c","description":"I'm Usman, a passionate software engineer and solution architect.","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":540}},"image":"https://media.daily.dev/image/upload/s--v2ZwqdLv--/f_auto/v1788496837/posts/HtZDmhcHl?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"comment":[{"@type":"Comment","text":"I’d add that any fixed system resource should be accessed by an interface, be it date time, file and folder access, Most of System.Runtime and even System.Random. Mocking those connections after the fact will be a rats nest of reflection and dark magiks","datePublished":"2026-09-10T08:40:45.792Z","url":"https://daily.dev/posts/HtZDmhcHl#c-5TwDCwUWJ","author":{"@type":"Person","name":"neil hatly","url":"https://daily.dev/neilhatly","image":"https://media.daily.dev/image/upload/s--NQzs28XS--/f_auto/v1734692205/avatars/avatar_NM0uvu3dPLeLBaVyW0bIO"}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/usmancode","name":"UsmanCode"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"UsmanCode","item":"https://daily.dev/squads/usmancode"},{"@type":"ListItem","position":3,"name":"You should stop using DateTime.Now in your code!"}]}
```

