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?

13
  • 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"); Commented Oct 12, 2017 at 7:04
  • 2
    The DateTime.ParseExact does not use a DateTimeFormatter so I don't think it knows the format of DateTimeFormatter("longdate") Commented Oct 12, 2017 at 7: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. Commented Oct 12, 2017 at 7:06
  • If you need a formatted string this should be enough: DateTime.Today.ToLongDateString() Commented Oct 12, 2017 at 7: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 Commented Oct 12, 2017 at 7:19

3 Answers 3

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")

Sign up to request clarification or add additional context in comments.

Comments

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.
}

2 Comments

your solution is not culture independent. Refer comments above. It appears this is a UWP issue.
yes, if you use CultureInfo like new CultureInfo((int)CultureTypes.AllCultures) then i think does not matter which culture is.
-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();
        }

4 Comments

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.
Thanks again @JohnChris but we are not using UTC. We use the culture of the local machine ... wherever that might be.
@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?
mydateString is not an invalid format.It is the standard long date format created by 'Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longdate")'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.