Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get Date From Computer

Tags:

date

c#

I'm trying to get the date into a string in C#. I want the format of:

2011.02.14

Which is, YYYY.MM.DD

Can someone please let me know the command for this? And also is there a way to do like Date(-1)?

Thanks.

like image 441
The Woo Avatar asked Feb 14 '11 06:02

The Woo


2 Answers

  • System.DateTime.Now.ToString("yyyy.MM.dd");
  • System.DateTime.Now.AddDays(-1)
like image 174
abmv Avatar answered Sep 20 '22 10:09

abmv


For the first:

string s = DateTime.Now.ToString("yyyy.MM.dd");

For the second, you'd need to be explicit about what Date(-1) should return, but I expect it involves var foo = someDate.Add(timespan); or var foo = someDate.Add{SomeInterval}(delta);

like image 31
Marc Gravell Avatar answered Sep 21 '22 10:09

Marc Gravell