40

Is it possible to add the word "at" between a Date and Time format? I tried to add it just like this "dddd, d MMM, yyyy at HH:mm" but the webapp is tranforming it into "aA" or "aP" depending of regional configuration CA or AU

Console.WriteLine(DateTime.Now.ToString("dddd, d MMM, yyyy at HH:mm"));

I forgot to say. I must be as a format string and no complex or concat functions.

jessegavin
  • 67,411
  • 26
  • 135
  • 162
Maximus Decimus
  • 4,175
  • 19
  • 55
  • 83

4 Answers4

90

Yes, you need to escape the word by putting it in ' marks dddd, d MMM, yyyy 'at' HH:mm

Custom DateTime string formatting

Darren Kopp
  • 71,547
  • 9
  • 71
  • 90
23
Console.WriteLine(DateTime.Now.ToString("dddd, d MMM, yyyy 'at' HH:mm"));
3

Improving @DaveDev answer using C# 6's String Interpolation

var now = DateTime.Now;
Console.WriteLine($"{now:dddd, d MMM, yyyy} at {now:HH:mm}");
asakura89
  • 501
  • 1
  • 11
  • 19
2

The other answers are far better, but:

var now = DateTime.Now;

var str = now.ToString("d MMM yyyy") + " at " + now.ToString("HH:mm");

or closer to your formatting:

var str = now.ToString("dddd, d MMM, yyyy") + " at " + now.ToString("HH:mm");
DaveDev
  • 38,095
  • 68
  • 199
  • 359