soundy
soundy

Reputation: 307

How to convert date string to specific date format?

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"

Upvotes: 0

Views: 3577

Answers (3)

Jayesh Sorathia
Jayesh Sorathia

Reputation: 1614

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>");
}

Upvotes: 1

Habib
Habib

Reputation: 223187

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"

Upvotes: 1

शेखर
शेखर

Reputation: 17614

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 

Upvotes: 1

Related Questions