-2

I want to format my date like :

From May 1 (10:02 pm) to May 3 (10:02 pm)

so I have this code:

Time.ToLocalTime().ToString("MMM. d, (h:m tt)")

Will this work?

Time is a regular DateTimeOffset variable in this case...

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215

2 Answers2

1

If I understand you right, you have 2 variables, let them name as

  DateTimeOffset time1 = ...
  DateTimeOffset time2 = ...

And you are looking for a representation like From May 1 (10:02 pm) to May 3 (10:02 pm). If it's your case, you can try string interpolation:

  string result = $"From {time1:MMMM d (h:mm tt)} to {time2:MMMM d (h:mm tt)}";

Note, that I've changed your format string as

  • MMMM for complete month name, i.e. April instead of Apr.
  • mm to have leading zero for minutes: 10:02 instead of 10:2.
  • . and , are removed (they are not used in the proposed outcome example)
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Ok I got it to work.

using System;
                
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        DateTimeOffset time = DateTimeOffset.UtcNow;
        string newtime = time.ToLocalTime().ToString("MMM. d, (h:m tt)");
        
        Console.WriteLine(newtime);
    }
}