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?
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);
}
}
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With