Date time format (Uppercase)

A

Arjen

Hello,

How can I make the first letter uppercase?

ToString("ddd. dd MMMM yyyy")
This ddd. will be for example monday, I want to see Monday.

Thanks!
 
J

Jon Skeet [C# MVP]

Arjen said:
How can I make the first letter uppercase?

ToString("ddd. dd MMMM yyyy")
This ddd. will be for example monday, I want to see Monday.

Thanks!

That's odd - the above gives "Mon" for me - I need "dddd" to get the
full day name. Either way, it begins with a capital letter. What
culture are you in?

You can always do post-processing, of course, and capitalise the first
letter that way...
 
A

Arjen

Whoops, I made a mistake. Sorry.

I see now "mon." but I want to see "Mon.".
Is that posible?

Thanks!
 
J

Jon Skeet [C# MVP]

Arjen said:
Whoops, I made a mistake. Sorry.

I see now "mon." but I want to see "Mon.".
Is that posible?

I believe that to do that, you would either need to do tricks with
CultureInfos, or (easier) just lowercase the first letter manually:

string original = date.ToString("...");
string lowered = Char.ToLower(original[0])+original.Substring(1);

Not the fastest code in the world, but it'll work and it's simple.
 
B

Bruno Jouhier [MVP]

I'm afraid that you don't have control over this and that you have to do
some string manipulations if you want to control it.

On the other hand, you should not try to control capitalization yourself
because it is culture dependent. For example, the names of the days are
capitalized in English (Monday, Tuesday, ...) but not in French (lundi,
mardi, ...). So, you will do the wrong thing if you capitalize them without
checking the culture.

Also, ddd will display "Mon" (capitalized), and dddd will produce "Monday"
in the usual English cultures. How did you get the "monday" result?

Bruno.
 
J

Jon Skeet [C# MVP]

Bruno Jouhier said:
I'm afraid that you don't have control over this and that you have to do
some string manipulations if you want to control it.

Actually, I've just come up with a slightly nicer way of doing it - you
only need to do the string manipulations once.

Basically, you clone an existing DateTimeFormatInfo and alter the
DayNames and AbbreviatedDayNames properties, and then pass that
DateTimeFormatInfo in when formatting the DateTime:

using System;
using System.Globalization;

class Test
{
static void Main()
{
DateTimeFormatInfo dtfi =
CultureInfo.CurrentCulture.DateTimeFormat;

dtfi = (DateTimeFormatInfo) dtfi.Clone();

dtfi.DayNames = LowerNames(dtfi.DayNames);
dtfi.AbbreviatedDayNames = LowerNames
(dtfi.AbbreviatedDayNames);

Console.WriteLine (DateTime.Now.ToString("ddd. dd", dtfi));
}

static string[] LowerNames (string[] old)
{
string[] ret = new string[old.Length];

for (int i=0; i < ret.Length; i++)
{
ret = Char.ToLower(old[0])+old.Substring(1);
}
return ret;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top