Solved

String was not recognized as a valid DateTime

Posted on 2014-09-17
4
6,177 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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
If you still get the same error try changing line 13 as below:

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

Author Comment

by:homeshopper
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now