-3

I want to store the current date and time in a variable using C#. How would I do that if it has to be in the format YYYY, MM, DD, HH, MM, SS? I.e. I know I can store midnight on January 1st, 2013 as;

var time= new DateTime(2013, 1, 1, 0, 0, 0);

How do I do this with the system date and time?

Al Woods
  • 113
  • 1
  • 3
  • 11
  • @AlWoods:- Please dont mind but this language is not appreciated here. And as far as your answer is concerned you may have received your answer now. Thanks!!! – Rahul Tripathi Sep 09 '13 at 18:51
  • @AlWoods This question is quite definitely a duplicate. I can see you're new here and also offended by my comment (which you shouldn't be), but you just don't understand the definition of a duplicate here. Just because the provided solutions aren't *precisely, exactly* what you're looking for instead of a spoon-fed answer, you should spend some time actually trying to figure it out instead of asking people to do it for you. – tnw Sep 09 '13 at 18:57
  • Please see [MSDN documentation for DateTime](http://msdn.microsoft.com/en-us/library/system.datetime.aspx). You'll find everything you need there in the examples section. – tnw Sep 09 '13 at 19:01
  • 1
    @Rahual - Sorry but a rude comment is getting a rude reply. Of course I googled it, and I obviously wasn't helped by google. – Al Woods Sep 09 '13 at 19:02
  • @tnw, not to double state myself, but I obviously would have googled it first. I am new to stack, but common sense says I looked around before asking a question. The other thread didn't help me, so I created a new one. – Al Woods Sep 09 '13 at 19:04
  • @AlWoods Because you didn't try very hard, or at all. If you'd spent more than 2 seconds googling, you would have found the documentation which I linked, or [here's another SO question that's exactly what youre asking here](http://stackoverflow.com/questions/32747/how-do-i-get-todays-date-in-c-sharp-in-8-28-2008-format), and [yet another](http://stackoverflow.com/questions/420623/how-to-convert-date-into-mm-dd-yy-format-in-c-sharp?rq=1). – tnw Sep 09 '13 at 19:06
  • I can tell you're new here. Look, I'm just telling you the rules of the road here. Anybody else here would tell you the exact same thing. Don't take it so personally, we all went over the bumps when we started on SO. – tnw Sep 09 '13 at 19:10

5 Answers5

9
var time = DateTime.Now;

Format the time when you retrieve it, not when you store it - i.e. -

string formattedTime = time.ToString("yyyy, MM, dd, hh, mm, ss");
Nick Raverty
  • 431
  • 2
  • 8
2

You need DateTime's static field Now:

var time = DateTime.Now;
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
2

You can try using the DateTime.Now like this:-

var time = DateTime.Now;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

Here it is:

var time = DateTime.Now;
BlackBear
  • 22,411
  • 10
  • 48
  • 86
1
DateTime currentTime = DateTime.Now;