Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String was not recognized as a valid DateTime

Posted on 2014-09-17
4
Medium Priority
?
9,565 Views
Last Modified: 2014-09-17
I have the following code and get an error:
String was not recognized as a valid DateTime
DateTime strResult = DateTime.ParseExact(dateString, oldFormat, provider);
I am trying to produce today's date in format day-month-year.
Thanks in advance for any help given.
var result = "";
    if (IsPost)
    {
        var firstName = Request["FirstName"];
        var lastName = Request["LastName"];
        var email = Request["Email"];
        var userData = firstName + "," + lastName +
            "," + email + Environment.NewLine;
        var dataFile = Server.MapPath("~/App_Data/data" + @Session["CustId"] + ".txt");
        //File.AppendAllText(@dataFile, userData);
        File.WriteAllText(@dataFile, userData);
        System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.CreateSpecificCulture("en-UK");
        string dateString = DateTime.Today.ToString();
        string oldFormat = "ddmmyy";
        DateTime strResult = DateTime.ParseExact(dateString, oldFormat, provider);
        result = "Information saved on " + strResult + "#";
    }

Open in new window

0
Comment
Question by:homeshopper
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40328344
Use this code sample and change you code accordingly:

Dim dtfi As New DateTimeFormatInfo()
dtfi.ShortDatePattern = "dd/MM/yyyy"
dtfi.DateSeparator = "/"
Dim strResult = Convert.ToDateTime(dateString, dtfi)
0
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 total points
ID: 40328380
You're trying to display the date in a given format?
ParseExact does the opposite operation : convert a text representing a date in a given format to a datetime value.

Try this:
string dateString = DateTime.Today.ToString("ddMMyy);
result = "Information saved on " + dateString + "#";

Open in new window

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40328392
If you still get the same error try changing line 13 as below:

string dateString = DateTime.Today.ToShortDateString();
0
 

Author Comment

by:homeshopper
ID: 40328430
Thankyou for your suggestions.
The following worked:
string dateString = DateTime.Today.ToString("dd-MM-yy");
result = "Information saved on " + dateString + "#";

Open in new window

The following code gives same error:
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.CreateSpecificCulture("en-UK");
        string dateString = DateTime.Today.ToShortDateString();
        string oldFormat = "ddmmyy";
        DateTime strResult = DateTime.ParseExact(dateString, oldFormat, provider);
        result = "Information saved on " + strResult + "#";

Open in new window

Thanks for the help given.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question