0
private string GetSystem()
{
    StringBuilder results = new StringBuilder();

    DateTimeFormatter[] basicFormatters = new[]
    {
        // Default date formatters
        new DateTimeFormatter("shortdate"),

        // Default time formatters
        new DateTimeFormatter("longtime"),
    };

    DateTime dateandTime = DateTime.Now;

    foreach (DateTimeFormatter formatter in basicFormatters)
    {
        // Format and display date/time.
        results.Append(formatter.Format(dateandTime));
        results.Append(" ");
    }           
    return results.ToString();
}

dateString = GetSystem();
format = "dd-MM-yyyy HH:mm:ss";
provider = new CultureInfo("en-IN");
try
{           
    result = DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture);
    Debug.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
    Debug.WriteLine("{0} is not in the correct format.", dateString);
}

I'm getting the error

"String was not recognized as a valid DateTime"

while running this code can anyone suggest some idea to resolve my problem.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3214224
  • 173
  • 1
  • 1
  • 13
  • What did the dateString look like when the error occured? maybe you are trying to parse the 30th of february or the 2nd of month 15. – Marcel B May 30 '16 at 06:35
  • What is your `dateString` _exactly_? Debug your code and tell us. Also be aware, you are not using `provider` _anywhere_ in your code. – Soner Gönül May 30 '16 at 06:46
  • 2
    His datestring is (you can see it in `GetSystem`) a concatenation of a `shorttime` string, a space, and a `longtime` string. Obviously `ParseExact` can't translate that with a format string of `dd-MM-yyyy HH:mm:ss` – Jcl May 30 '16 at 06:48
  • Note that `dd-MM-yyyy` is a fairly odd format - `yyyy-MM-dd` is much more common. – Jon Skeet May 30 '16 at 07:09
  • 1
    Off topic: Please remove irrelevant tags. This has nothing to do with WPF, UWP-Xaml, winrt-xaml and if we are pushing it, even uwp! – Nahuel Ianni May 30 '16 at 08:29
  • This is the output of dateString = "‎30‎-‎05‎-‎2016 ‎14‎:‎54‎:‎18 " . I also tried by Trim the string so, that empty space got removed still getting the same error – user3214224 May 30 '16 at 09:26
  • the dateString you provide looks fine until converted into ASCII or HEX. This is what 30 should look like in ASCII: `034 051` and this is your string: `034 226 128 142 051 048 226 128`. The Problem is either in the DateTimeFormatter or in the StringBuilder. – Marcel B May 30 '16 at 12:10

1 Answers1

1

In UWP apps, while using Format Template like shortdate or longtime, by default it will use user's default global context which are shown in Settings under Time & Language. And in my computer, they are set as following:
enter image description here So when I use your GetSystem method in my side, the dateString is like

6‎/‎1‎/‎2016 ‎1:‎44‎:‎43‎ ‎PM

and obviously this can't be parsed with format = "dd-MM-yyyy HH:mm:ss";. So I think using a fixed custom date and time format string here is not a good practice.

Then even when the format of your dateString matches the format you've used in DateTime.ParseExact method, you will also get the error: String was not recognized as a valid DateTime.

This is because, when we use DateTimeFormatter.Format method, there are some invisible 8206 characters in its return value. So your dateString looks like 30‎-‎05‎-‎2016 ‎14‎:‎54‎:‎18, but actually it's not 30-05-2016 14:54:18. To see this clearly, we can convert the dateString to char array. Here using "shortdate" template for example:

var dateString = new DateTimeFormatter("shortdate").Format(DateTime.Now);
var array = dateString.ToCharArray();
foreach (var item in arry)
{
    Debug.WriteLine(item);
}

And the char array will like:
enter image description here

So to solve your problem, I'd suggest you use The General Date Long Time ("G") Format Specifier.

The "G" standard format specifier represents a combination of the short date ("d") and long time ("T") patterns, separated by a space.

You can use this format specifier like following:

var dateString = DateTime.Now.ToString("G");

And then convert string to DateTime like:

var result = DateTime.Parse(dateString);

Or

var result = DateTime.ParseExact(dateString, "G", null);

the provider here is null which represents the CultureInfo object that corresponds to the current culture is used. If we use wrong culture here, we will also get String was not recognized as a valid DateTime exception.

If you do want to use dd-MM-yyyy HH:mm:ss format, you can use some code like:

var dateString = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");

And the convert is as same as above, only in this scenario the provider parameter is not important.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49