Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date from this string?

I have this string:

\tid <[email protected]>; <b>Tue, 6 Oct 2009 15:38:16</b> +0100

and I want to extract the date (emboldened) to a more usable format, e.g. 06-10-2009 15:38:16

What would be the best way to go about this?

like image 557
leddy Avatar asked May 02 '26 03:05

leddy


2 Answers

Regex might be overkill. Just Split on ';', Trim(), and call Date.Parse(...), It will even handle the Timezone offset for you.

using System;

namespace ConsoleImpersonate
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "\tid [email protected]; Tue, 6 Oct 2009 15:38:16 +0100";
            var trimmed = str.Split(';')[1].Trim();
            var x = DateTime.Parse(trimmed);

        }
    }
}
like image 109
Jan Bannister Avatar answered May 03 '26 15:05

Jan Bannister


You can try this code (with possible adjustments)

Regex regex = new Regex(
      ";(?<date>.+?)",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

var dt=DateTime.Parse(regex.Match(inputString).Groups["date"].Value)
like image 28
Nikolay R Avatar answered May 03 '26 15:05

Nikolay R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!