'How to format a date with parentheses
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...
Solution 1:[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
MMMMfor complete month name, i.e.Aprilinstead ofApr.mmto have leading zero for minutes:10:02instead of10:2..and,are removed (they are not used in the proposed outcome example)
Solution 2:[2]
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitry Bychenko |
| Solution 2 | Claire Bilski |
