Вопрос

I'm new to coding in asp.net and I'm struggling to format the date I've managed to pull from our CRM content pages' live date. I've wrote the following code that manages to pull and display the date the page was created:

TheDate = DR["LiveDate"].ToString();

But it formats the date and time like this:

13/11/2012 00:00:00

How can I modify my above code to just display the date like so:

Tuesday, 13 November 2012

Or, just without the time

13/11/2012

Thanks,

Это было полезно?

Решение

I assume that DR is a DataRow, isn't it?

If DR["LiveDate"] is already a DateTime column don't convert it to a localized string via ToString. Just cast it accordingly, for example via Field extension method:

DateTime dt = DR.Field<DateTime>("LiveDate");

If it's not a DateTime but a string you have to parse it to DateTime:

DateTime dt = DateTime.Parse(DR.Field<String>("LiveDate"));

If you want Tuesday, 13 November 2012 as result use:

string result = dt.ToString("dddd, dd MMMM yyyy", CultureInfo.InvariantCulture);

If you want 13/11/2012 use:

string result = dt.ToShortDateString(); 

or

string result = dt.ToString("d");

if your culture uses / as separator

otherwise

string result = dt.ToString("dd/MM/yyyy",  CultureInfo.InvariantCulture);

Further informations: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Другие советы

for your second question, try

TheDate = DR["LiveDate"].ToString("dd/MM/yyyy")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top