Cannot convert string to DateTime in uwp

Error processing SSI file

Answers

  1. Phillips

    • 2020/4/25

    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: 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:

    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.

  2. De Luca

    • 2016/12/22

    In UWP apps, while using Format Template like shortdate or longtime , by default it will use user's default global context which are shown 

  3. Wesley

    • 2017/12/7

    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.

  4. Kaiser

    • 2015/6/23

    Cannot Convert String To Datetime In Uwp. Here is the code I test for example: import { Injectable } from '@angular/core'; import 

  5. White

    • 2017/3/3

    The following example illustrates the use of the DateTime.Parse method to convert a string into a DateTime. This example uses the culture associated with the current thread. If the CultureInfo associated with the current culture cannot parse the input string, a FormatException is thrown.

  6. Aayan

    • 2020/11/1

    Learn techniques to parse strings that represent dates and times to create a cannot parse the input string, a FormatException is thrown.

  7. Allen

    • 2020/10/27

    Question: How can I convert a string value to a datetime value? i.e. do the equivalent of System.Convert.ToDateTime(<string value>); Rules: You cannot use the System.Convert statement/class, since Microsoft removed the ability

  8. Bode

    • 2015/1/5

    WriteLine("Unable to convert '{0}'.", dateString); } // Parse string with date but no time component. dateString = "2/16/2008"; try { dateValue = DateTime.Parse 

  9. Sylas

    • 2021/9/20

    If the value is null then it returns ArgumentNullException and same way if value contains some invalid date format then it returns FormatException. The format must match with string date time or it throws FormatException. So to overcome this issue you can use string format array which has some possibilities.

  10. Aldo

    • 2017/10/31

    This FAQ explains the topic "How to resolve the error message 'Cannot implicitly convert type 'string' to 'System.DateTime' ' when using a DataReader"

  11. Branson

    • 2015/5/3

    I'm trying to convert 3 date dropdowns I have inside my create userwizard to a date format to post online, but I get Cannot convert type 'string' to 'System.DateTime' Here's what I have.

  12. Sanders

    • 2021/6/27

    string iDate = "05/05/2005"; DateTime oDate = Convert.ToDateTime(iDate); MessageBox.Show(oDate.Day + " " + oDate.Month + " " + oDate.Year );

  13. Giordano

    • 2017/1/28

    For strings that contain time zone information, tries to prevent the conversion of a date and time string to a DateTime value that represents a local time with its Kind property set to DateTimeKind.Local. Typically, such a string is created by calling the DateTime.ToString(String) method and by using the "o", "r", or "u" standard format specifier.

  14. Raylan

    • 2018/9/28

    String: 30/10/2018 11:06:30 AM I have tried the below codes but it isn't working. Pls help row(“LogTime”)(“dd/MM/yyyy HH:mm:ss”) Convert.

  15. Evans

    • 2016/10/16

    Both these methods return a double value after converting the string value to double. Syntax with Explanation: The syntax of Convert.ToDouble() method in both 

  16. Prifti

    • 2017/3/18

    Unable to cast object of type 'System string to type 'System char. Controls; SDK; Software; Store; UWP Universal Windows Plattform; vb. TextBox'.

  17. Stewart

    • 2020/12/31

    Unable to import field with a name that is not specified as a literal string. An exception is thrown when the NormalContentSource of a 

Comments are closed.

More Posts