0

Maybe this is a stupid question but I have a string with a date that looks like this: 20190516060354.000000+000 and wonder if I can automatically convert it into a date format like dd-mm-YYYY hh:mm.

Important: I don't want to parse the date manually.

The date comes from the WMIv2 class MSFT_MpComputerStatus => AntivirusSignatureLastUpdated

Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
creg
  • 154
  • 3
  • 13
  • 1
    'I don't want to parse the date manually' - what do you mean? Take your string put it into a `DateTime` with `ParseExact` and re-format it with DateTime's ToString("dd-mm-YYYY hh:mm") method – nilsK May 17 '19 at 08:06

2 Answers2

2

Use the appropriate System.Management Utility method

For exactly this scenario:

System.Management.ManagementDateTimeConverter.ToDateTime(string) method.

e.g. In PowerShell

[System.Management.ManagementDateTimeConverter]::ToDateTime('20190516060354.000000+000')       
Wednesday, May 15, 2019 11:03:54 PM

or C#

using System;
using System.Management;
System.DateTime dt = ManagementDateTimeConverter.ToDateTime("20190516060354.000000+000");
Console.WriteLine(dt);
Community
  • 1
  • 1
rburte
  • 627
  • 6
  • 11
1

DateTime.ParseExact / TryParseExact

You have to parse it manually... using the tools .Net has.

You can do the following:

string inputDate = "20190516060354.000000+000";
DateTime parsed = DateTime.ParseExact(inputDate, "yyyyMMddHHmmss.ffffffzz0", CultureInfo.InvariantCulture);
string output = parsed.ToString(); // Or .ToString("dd-mm-YYYY HH:mm")

You can use the method TryParse if you are unsure about the format and log the errors to adjust it. Or use the overload that admits multiple formats in Parse/TryParse methods.

More info

https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1

I still have doubts on what the last 0 means or +000 with three zeros means.