0

I get date string value(3/13/2013 12:00:00AM) from database and i need to convert like this format (yyyy-mm-dd). Please help me solve this.

string targetdate = "3/13/2013 12:00:00AM";(getting date value from DB)
DateTime lastdate = DateTime.ParseExact(targetdate, "yyyy-mm-dd", 
                  System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);

And I tried

Iformatprovider = null. 

but i getting same error "String was not recognized as a valid DateTime"

3 Answers 3

1

I think the problem is with the date time

"3/13/2013 12:00:00AM"

It should not be 12:00:00AM.

It should be 12:00:00PM.

Example

  string targetdate = "3/13/2013 11:59:59AM";
  DateTime lastdate = DateTime.ParseExact(targetdate,
                               "M/d/yyyy HH:mm:sstt",
                               System.Globalization.CultureInfo.InvariantCulture);

  lastdate=lastdate.AddSeconds(1);

You will get

  3/13/2013 12:00:00 AM

I would suggest you to cast it in the database end.

If you are using sql server then

Example

The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time:

CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

The result would look something like this:

Nov 04 2011 11:45 PM
11-04-11
11-04-2011
04 Nov 11
04 Nov 2011
04 Nov 2011 11:45:34:243 
Sign up to request clarification or add additional context in comments.

Comments

1

First you need to convert your date string to DateTime type object using the format "M/d/yyyy HH:mm:sstt" later you can get the formatted string using "yyyy-MM-dd". (You used lower case m for month, it should be upper case M for month.

string targetdate = "3/13/2013 12:00:00AM";
DateTime lastdate = DateTime.ParseExact(targetdate, 
                       "M/d/yyyy hh:mm:sstt", 
                       System.Globalization.CultureInfo.InvariantCulture);

string newFormat = lastdate.ToString("yyyy-MM-dd");

newFormat would contain "2013-03-13"

6 Comments

getting same error(String was not recognized as a valid DateTime) in that line "DateTime lastdate ". I did everything what u said.
@soundy, strange, It must be the culture issue, try the edited code, with System.Globalization.CultureInfo.InvariantCulture as the last parameter in DateTime.ParseExact, this should work.
@soundy, I just test the above code in Visual studio and it is working fine, are you sure you are not passing targetdate from some where else, because that seems to change. Try this code in a simple Console application and see if it works.
targetdate value get directly from DB(mysql) like dt.Rows[0]["actual_target"].ToString().Trim()but in DB the value was like this "2013-03-13". i dont know why its coming like this . please
@soundy, put a break point and see what are you passing to DateTime.ParseExact, it seems your value is different from the string, also why are you getting the data back from the db in string format, you can get the data in DateTime type object
|
1

DateTime conversion is really easy in .NET if you know which DateTime format you have and which you want to convert to.

Here is an example of this:

String origionalDate = "12/20/2013"; // Format : MM/dd/yyyy
string origionalFormat = "MM/dd/yyyy";
string convertInToFormat="dd/MM/yyyy";
String convertedDate;
DateTime date;

if (DateTime.TryParseExact(
        origionalDate, 
        origionalFormat, 
        CultureInfo.InvariantCulture, 
        DateTimeStyles.None, 
        out date))
{
    convertedDate = date.ToString(convertInToFormat);
    Response.Write(
        $"<b>Origional DateTime Format ({origionalFormat}) : </b>{origionalDate}");
    Response.Write("<br/>");
    Response.Write(
        $"<b>Converted DateTime Format ({convertInToFormat}) : </b>{convertedDate}");
}
else
{
    Response.Write($"<b>Not able to parse datetime {origionalDate}.</b>");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.