---
title: "How important is it to use a variable for DateTime.Today when concerned about performance?"  
description: "How important is it to use a variable for DateTime.Today when concerned about performance?"  
author: "Anonymous User"  
published: 2013-04-06  
updated: 2013-04-06  
canonical: https://www.mindstick.com/forum/724/how-important-is-it-to-use-a-variable-for-datetime-today-when-concerned-about-performance  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How important is it to use a variable for DateTime.Today when concerned about performance?

Hi All!\
It was in [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) to a post that contained the code:\
var first = new [DateTime](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty)(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1);var last = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1);If I [am looking](https://answers.mindstick.com/qa/115463/i-am-looking-for-to-joining-the-mindstick-internship-program-how-to-apply) to [improve performance](https://www.mindstick.com/forum/160287/how-can-parameterized-stored-procedures-improve-performance-compared-to-dynamic-sql-queries), how important is it to store DateTime.Today in a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) instead of calling it [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) times? And roughly how many uses of DateTime.Today would justify creating a variable for it?\
Edit: I realize I should test my [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to see if there are performance problems first before worrying about something as trivial as this. For the sake of this [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time), assume that I have already done this and determined that additional [optimization](https://yourviews.mindstick.com/view/85459/what-is-conversion-rate-optimization-and-how-to-get-started) is needed.\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!\

## Replies

### Reply by AVADHESH PATEL

Hi Tanhuj!\
10,000 DateTime.Today calls and assignment to local variable: 0.0125781 seconds.\
10,000 Assignment only operations: 0.0001062 seconds.\
**code as given below**\

```
var sw = new Stopwatch();DateTime date1 = DateTime.Today;DateTime date2 = DateTime.Today;sw.Start();for (int i=0; i<10000; i++)    date1 = DateTime.Today;sw.Stop();Debug.Print(sw.Elapsed.ToString());sw.Reset();sw.Start();for (int i=0; i<10000; i++)    date2 = date;sw.Stop();Debug.Print(sw.Elapsed.ToString());
```

\
I hope it resolve your problem


---

Original Source: https://www.mindstick.com/forum/724/how-important-is-it-to-use-a-variable-for-datetime-today-when-concerned-about-performance

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
