code master
code master

Reputation: 2026

How to format 2010-11-24 date in C#?

I am using

((DateTime)newsItem.Date).ToString(@"yyyy MM dd") 

which gives me 2010 11 24 but not 2010-11-24.

I want dashes in between the numbers of date.

Upvotes: 12

Views: 17644

Answers (2)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60754

Have you tried

((DateTime)newsItem.Date).ToString(@"yyyy-MM-dd");

Also, since you format the time part away anyway in the ToString, you don't need to add .Date, and you can change it to

((DateTime)newsItem).ToString(@"yyyy-MM-dd");

Upvotes: 32

Related Questions