Vijaychandar
Vijaychandar

Reputation: 714

C# Datetime format conversion

I have a conversion problem with datetime. I have a date string as MM/dd/yyyy. Now I need to convert it to yyyy-MM-dd.

But I'm facing some error. Please help

 public static DateTime ToDBDateTime(string _dateTime)
 {
        string sysFormat = "MM/dd/yyyy hh:mm:ss tt";

        string _convertedDate = string.Empty;

        if (_dateTime != null || _dateTime != string.Empty)
        {
            _convertedDate = DateTime.ParseExact(_dateTime, sysFormat, System.Globalization.CultureInfo.InvariantCulture).ToString(_toDBDateFormat);
            //_convertedDate = Convert.ToDateTime(_dateTime).ToString(_toDBDateFormat);
           /// Debug.Print(sysFormat);
        }

        return Convert.ToDateTime(_convertedDate);
    }

And I want to know that is there is any way to pass the datetime in various formats and it would return the expected format.

E.g.: if I pass date as dd/MM/yyyy or MM/dd/yyyy, the above function would return the date in format as yyyy-MM-dd.

Please provide some suggestion to solve datetime issues.

Upvotes: 1

Views: 8967

Answers (4)

Samy S.Rathore
Samy S.Rathore

Reputation: 1823

As you have a date in a string with the format "MM/dd/yyyy" and want to convert it to "yyyy-MM-dd" you could do like this:

DateTime dt = DateTime.ParseExact(dateString, "MM/dd/yyyy", CultureInfo.InvariantCulture);
        dt.ToString("yyyy-MM-dd");

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1499770

I have a date string as MM/dd/yyyy

Right... and yet you're trying to parse it like this:

string sysFormat = "MM/dd/yyyy hh:mm:ss tt";
...
_convertedDate = DateTime.ParseExact(_dateTime, sysFormat,
                                     CultureInfo.InvariantCulture)

You need to give a format string which matches your input - so why are you including a time part? You probably just want:

string sysFormat = "MM/dd/yyyy";

However, that's not the end of the problems. You're then converting that DateTime back into a string like this:

.ToString(_toDBDateFormat)

... and parsing it once more:

return Convert.ToDateTime(_convertedDate);

Why on earth would you want to do that? You should avoid string conversions as far as possible. Aside from anything else, what's to say that _toDBDateFormat (a variable name which raises my suspicions to start with) and Convert.ToDateTime (which always uses the current culture for parsing) are going to be compatible?

You should:

  • Work out how you want to handle being given an empty string or null, and just return an appropriate DateTime then
  • Otherwise, just parse using the right format.

This part of your question also concerns me:

E.g.: if I pass date as dd/MM/yyyy or MM/dd/yyyy, the above function would return the date in format as yyyy-MM-dd.

There's no such thing as "the date in format as yyyy-MM-dd". A DateTime is just a date and time value. It has no intrinsic format. You specify how you want to format it when you format it. However, if you're using the value for a database query, you shouldn't be converting it into a string again anyway - you should be using parameterized SQL, and just providing it as a DateTime.

Upvotes: 4

Rusty
Rusty

Reputation: 1303

Use the inbuilt tostring like this:

Convert.ToDateTime(_convertedDate).ToString("MM/dd/yyyy") or whatever format you want.

Upvotes: 1

Milind Thakkar
Milind Thakkar

Reputation: 980

I tried this and its working fine.

DateTime date1 = new DateTime(2009, 8, 1);
date1.ToString("yyyy-MM-dd hh:mm:ss tt");

You can apply any format in this ToString.

Hope that helps Milind

Upvotes: 0

Related Questions