2

In UWP, formatting a date as a longdate string like this

string myDateString = new DateTimeFormatter("longdate").Format(DateTime.Today);

gives myDateString = "‎Thursday‎, ‎12‎ ‎October‎ ‎2017"

Trying to convert it back like this

DateTime myDate = DateTime.Parse(myDateString, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal);

throws System.FormatException

Trying to convert it back like this

DateTime myDate = DateTime.ParseExact(myDateString, "longdate", CultureInfo.CurrentCulture);

Also throws System.FormatException

I then set my machine to US. The value of myDateString = "‎Thursday‎, ‎October‎ 12‎ ‎‎2017"

but when I try it convert it back to a datetime this also throws a System.FormatException.

How should I convert a long date string to a datetime in C# using the current culture?

Vague
  • 2,198
  • 3
  • 18
  • 46
  • whats your long date format? mm/dd/yyyy? Get the datetime, DateTime d = DateTime.Now; then convert it to a string in any format you want, string s = d.ToString("dd/MM/yyyy-HH:mm:ss.fff"); – JohnChris Oct 12 '17 at 07:04
  • 2
    The `DateTime.ParseExact` does not use a `DateTimeFormatter` so I don't think it knows the format of `DateTimeFormatter("longdate")` – EpicKip Oct 12 '17 at 07:05
  • 1
    I assume this is UWP? If so, I would suggest tagging it that way. Additionally, please provide the actual value you're getting for `myDateString`. – Jon Skeet Oct 12 '17 at 07:06
  • If you need a formatted string this should be enough: DateTime.Today.ToLongDateString() – Artem Oct 12 '17 at 07:07
  • As shown in my example there is no issue converting a date to a string. The issue is converting back from a long date string to a datetime – Vague Oct 12 '17 at 07:19
  • edited as suggested by @Jon Skeet – Vague Oct 12 '17 at 07:28
  • If you want to parse `longdate` to any culture that would be in that machine, you need to create custom parser that will do that, or just don't use `longdate` format while transferring data. – SᴇM Oct 12 '17 at 08:29
  • Is `new DateTimeFormatter("longdate").Format(DateTime.Today)` different from `DateTime.Today.ToString("D")`? And if not, wouldn't it be as simple as `DateTime myDate = DateTime.ParseExact(myDateString, "D", CultureInfo.CurrentCulture);` to get the `DateTime` value back? – Corak Oct 12 '17 at 08:31
  • @Corak your example also throws 'System.FormatException' – Vague Oct 12 '17 at 08:42
  • @Corak please propose as solution. Refer https://stackoverflow.com/questions/37518583/cannot-convert-string-to-datetime-in-uwp – Vague Oct 12 '17 at 09:00
  • @Vague - right... because in the string you get (and posted here), there are some weird invisible unicode characters, which is a problem when trying to parse it back. – Corak Oct 12 '17 at 09:02
  • @Vague - well, "use `ToString("D")` (or `"G"` or whatever) instead" doesn't *really* answer the question how to get a `DateTime` back from a `DateTimeFormatter` formatted date. It's weird that they even decided to put invisible characters in the result but it's a glaring mistake that they provide a way to format something but no (obvious) way to parse that formatted something back. Especially for something as "critical" as `DateTime` with all the special cases and culture dependent stuff. – Corak Oct 12 '17 at 09:42
  • Well, they say [it's not a bug](https://social.msdn.microsoft.com/Forums/en-US/0345aee8-1ca7-4716-97ae-9fe288e0e70f/datetimeformatter-bug?forum=winappswithcsharp). And for parsing back you're responsible yourself to remove the `(char)8206`... nice... – Corak Oct 12 '17 at 09:52

3 Answers3

0
string sd = "‎Thursday‎,‎ ‎October‎ 12, ‎2017";
sd = DateTime.Now.ToString("dddd, MMMM dd, yyyy", new CultureInfo("en-US"));
DateTime myDate;
if (DateTime.TryParseExact(sd,"dddd, MMMM dd, yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out myDate))
{
   Console.WriteLine(myDate);    //if format accepted.
}
Saber
  • 5,150
  • 4
  • 31
  • 43
deniz
  • 36
  • 3
  • your solution is not culture independent. Refer comments above. It appears this is a UWP issue. – Vague Oct 12 '17 at 09:25
  • yes, if you use CultureInfo like new CultureInfo((int)CultureTypes.AllCultures) then i think does not matter which culture is. – deniz Oct 12 '17 at 10:43
0

@Jay Zuo explains in Cannot convert string to DateTime in uwp

when we use DateTimeFormatter.Format method, there are some invisible 8206 characters in its return value.

So as @Corak suggested, don't use DateTimeFormatter, use ToString("D")

Vague
  • 2,198
  • 3
  • 18
  • 46
-1

Read over some DateTime formats... DateTime Formats

Stack post that is related: here

Basics on datetime: basics

Example on using DateTime with culture

A basic example:

DateTime d = DateTime.Now;
DateTime ut = d.ToUniversalTime();
// Defines a custom string format to display the DateTime value.
// zzzz specifies the full time zone offset.
  String format = "MM/dd/yyyy hh:mm:sszzz";

String utcstr = utcdt.ToString(format);
      Console.WriteLine(utcstr);

Edit: Small console app example

static void Main(string[] args)
        {
            string myDateString = "Thursday, 12 October 2017";
            //Why use the above just get a new one for today in the correct format
            //Or create your own converter
            DateTime date = DateTime.Now;

            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            //or
            var culture = System.Globalization.CultureInfo.CurrentCulture;

            string test = currentCulture.ToString();

             Console.WriteLine(date.ToString(CultureInfo.GetCultureInfo(test)));
            Console.ReadLine();
        }
JohnChris
  • 1,360
  • 15
  • 29
  • Thank you for the suggestion @JohnChris but we cannot specify a date format like that as we must use the culture of the users machine. – Vague Oct 12 '17 at 07:17
  • Thanks again @JohnChris but we are not using UTC. We use the culture of the local machine ... wherever that might be. – Vague Oct 12 '17 at 07:26
  • @Vague Okay.. i put for current culture... but I dont understand why u want to use myDateString, if its an invalid format. A solution to this would be to build your own converter to get the date, month year and put in correct format, Would you like me to write you a converter? – JohnChris Oct 12 '17 at 07:40
  • 1
    mydateString is not an invalid format.It is the standard long date format created by 'Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longdate")' – Vague Oct 12 '17 at 07:56