Solved

String was not recognized as a valid DateTime

Posted on 2014-09-17
4
8,491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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