3

So I had to convert a date in the format of (MM-dd-yyyy) to (dd-MMM-yyyy). What I ended up doing was this...

string strProvisionalDate = "04-22-2001";
string strFormat = "MM-dd-yyyy";
DateTime dtProvisional;
DateTime.TryParseExact(strProvisionalDate, strFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtProvisional);
string strProvisionalDateConverted = dtProvisional.ToString("dd-MMM-yyyy");
string strFormatConverted = "dd-MMM-yyyy";
DateTime dtProvisionalConverted;
DateTime.TryParseExact(strProvisionalDateConverted, strFormatConverted, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtProvisionalConverted);

Basically, I converted it to DateTime, converted that to a string in the format I wanted, then converted that back to DateTime. It works, but I was curious to ask if there was a better way to do this...it doesn't seem very elegant.

edit: Turns out, in this dtProvisional and dtProvisionalConverted end up being the same. So my new question would be, how can I convert a string in the format of MM-dd-YYYY to a DateTime in the format of dd-MMM-yyyy? dtProvisional is going into a SQL database, and it has to be in Date format.

5
  • Oh crap, you are right. So here is my followup question...I need the dtProvisional to be in the format of "dd-MMM-yyyy", how could I do that? Commented Jun 9, 2011 at 20:28
  • I don't understand why you're doing the last TryParseExact call... you already had that DateTime object, why take it to a string and then back to the DateTime object again?
    – Tim
    Commented Jun 9, 2011 at 20:29
  • What is the databae type of the field in SQL Server? SQL Server stores dates in a similar fashion and is fairly tolerant of being passed well-formed string values of dates.
    – ben f.
    Commented Jun 9, 2011 at 20:47
  • The database type is Date. And the client wants all dates in dd-MMM-yyyy format. If I pass SQL Server a string such as "12-Feb-1999", would it accept it as a date? Commented Jun 9, 2011 at 20:49
  • What I believe is that if the SQL Server datatype is Date, it really doesn't matter what format you pass it, the client's format will only be important in whatever application actually uses the data, because in the end, SQL Server doesn't store date format, but (even in queries) you can control date format when you want to vary from the standard format (I do not know of the top of my head whether SQL Server has a configurable standard format or not).
    – ben f.
    Commented Jun 9, 2011 at 20:51

2 Answers 2

6

The DateTime object doesn't store date/time as a format, it stores it as a number of ticks since the epoch. There should be no difference between dtProvisional and dtProvisionalConverted DateTime as object (their undelying value should be the same). The string format can always be changed when outputting the DateTime to a string using the String Format functionality.

See the remarks section on this MSDN article.

I this will format your date correctly for outputting:

String.Format("{0:dd-MMM-yyyy}", dt);

2
  • Right, I was so excited when I thought I figured out that I didn't even notice that dtProvisional and dtProvisionalConverted where the same. Commented Jun 9, 2011 at 20:34
  • Since they are the same you can just use dtProvisional.ToString(format)
    – BrunoLM
    Commented Jun 9, 2011 at 20:46
0

was searching details on similar line.... that worked for me....

DateTime IncentiveDate;
    /// <summary>
    /// Input Format: ddMMyy eg. 070711
    /// Output Format: dd-MMM-YYYY eg. 07-JUL-2011
    /// </summary>
    public string IncentiveDateAsString
    {
        set
        {
            DateTime.TryParseExact(value, "ddMMyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out IncentiveDate);
        }
        get
        {
            return IncentiveDate.ToString("dd-MMM-yyyy");
        }
    }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.