Reformatting in C #

I have several lines that look like this:

where S represents seconds and M represents minutes

"MM Minutes and XX Seconds"
"MM Minutes"
"SS Seconds"
":SS"

      

How would I convert this to one of the following: "MM: SS" string format TimeSpan format

Any help is appreciated! Thank!

+3


source to share


2 answers


You can use TempusReader . This is an open source project where I started to tackle this exact problem (and teach myself how to use Parsley ).

Take a look at the examples on the GitHub page and see if it suits your needs. It will work for words like and

in your first example. For example:



TimeSpan x = new Time("2 days, 7 hours, 12 mins and 52 seconds") // 2.07:12:52

      

In addition, an object TempusReader.Time

can be implicitly transferred to TimeSpan

.

+7


source


you can do something like this if you want to test with DateTime.Now eg



DateTime currentTime = DateTime.Now;
TimeSpan duration = DateTime.Now.Subtract(currentTime);
MessageBox.Show(string.Format("{0:D2}:{1:D2}", duration.Minutes, duration.Seconds));

      

0


source







All Articles