Solved

String was not recognized as a valid DateTime

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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