Solved

String was not recognized as a valid DateTime

Posted on 2014-09-17
4
7,025 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 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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