Parth Bhatt
Parth Bhatt

Reputation: 19469

How to get current date in 'YYYY-MM-DD' format in ASP.NET?

How to get current date in 'YYYY-MM-DD' format in ASP.NET ?

Upvotes: 30

Views: 181569

Answers (6)

Niraj Trivedi
Niraj Trivedi

Reputation: 2880

var formatedDate = DateTime.Now.ToString("yyyy-MM-dd",System.Globalization.CultureInfo.InvariantCulture);

To ensure the user's local settings don't affect it

Upvotes: 1

Jonathan
Jonathan

Reputation: 2358

Might be worthwhile using the CultureInfo to apply DateTime formatting throughout the website. Insteado f running around formatting whever you have to.

CultureInfo.CurrentUICulture.DateTimeFormat.SetAllDateTimePatterns( ...

or

CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"; 

Code should go somewhere in your Global.asax file

protected void Application_Start(){ ...

Upvotes: 1

Student
Student

Reputation: 3509

try ToString method for your desirer format use

DateTime.Now.ToString("yyyy-MM-dd"); 


OR you can use it with your variable of DateTime type

dt.ToString("yyyy-MM-dd");


where dt is a DateTime variable

Upvotes: 4

Humberto
Humberto

Reputation: 7199

<%= DateTime.Now.ToString("yyyy-MM-dd") %>

Upvotes: 6

Sean Carpenter
Sean Carpenter

Reputation: 7731

The ToString method on the DateTime struct can take a format parameter:

var dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
// dateAsString = "2011-02-17"

Documentation for standard and custom format strings is available on MSDN.

Upvotes: 7

goenning
goenning

Reputation: 6654

Which WebControl are you using? Did you try?

DateTime.Now.ToString("yyyy-MM-dd");

Upvotes: 60

Related Questions