raklos
raklos

Reputation: 28545

convert a string to a date format

I have a string like this "10/13/2009 12:00:00 AM"

how can i convert it to YYYYMMDD format using c#

Upvotes: 5

Views: 3385

Answers (5)

Neil Busse
Neil Busse

Reputation: 149

You could try something like this:

string myDate = "10/13/2009 12:00:00 AM";
DateTime result = new DateTime(DateTime.Now.Year, 1, 1);
DateTime.TryParse(myDate, out result);
string output = result.ToString("yyyyMMdd");

Upvotes: 0

Charles Bretana
Charles Bretana

Reputation: 146409

If your string is a valid datetime format that .Net can understand, all you need is:

   DateTime.Parse(yourString).ToString("yyyyMMdd")

EDITED: Many reasonable datetime formats are understandable by .Net without an explicit format specification, but if your specific one is not, then you will need to use an explicit format specifier.

   DateTime.ParseExact(yourString, format, 
         CultureInfo.InvariantCulture)).ToString("yyyyMMdd")

Upvotes: 2

Chris Love
Chris Love

Reputation: 3893

Those are pretty good solutions, but if you pass something in that does not match the pattern they will toss Exceptions. I like to use the SmartDate class from CSLA, http://www.lhotka.net/cslanet/download.aspx, myself. It deals with nulls and invalid values nicely. The TryStringToDate function is where the magic happens:

    private static bool TryStringToDate( string value, EmptyValue emptyValue, ref DateTime result )
    {
        DateTime tmp;
        if( String.IsNullOrEmpty( value ) )
        {
            if( emptyValue == EmptyValue.MinDate )
            {
                result = DateTime.MinValue;
                return true;
            }
            result = DateTime.MaxValue;
            return true;
        }
        if( DateTime.TryParse( value, out tmp ) )
        {
            result = tmp;
            return true;
        }
        string ldate = value.Trim().ToLower();
        if( ldate == "SmartDateT" ||
            ldate == "SmartDateToday" ||
            ldate == "." )
        {
            result = DateTime.Now;
            return true;
        }
        if( ldate == "SmartDateY" ||
            ldate == "SmartDateYesterday" ||
            ldate == "-" )
        {
            result = DateTime.Now.AddDays( -1 );
            return true;
        }
        if( ldate == "SmartDateTom" ||
            ldate == "SmartDateTomorrow" ||
            ldate == "+" )
        {
            result = DateTime.Now.AddDays( 1 );
            return true;
        }
        return false;
    }

So you should ultimately wind up with something like this:

        SmartDate dt = new SmartDate(input); 
        dt.FormatString = outputFormat;
        string output = dt.Text; 
        Console.WriteLine(output);

Upvotes: 0

zSynopsis
zSynopsis

Reputation: 4854

string s =  String.Format("{0:yyyyMMdd}", Convert.ToDateTime("10/13/2009 12:00:00 AM"));

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499810

Work out the two formats you want, then use:

DateTime dt = DateTime.ParseExact(input, inputFormat, 
                                  CultureInfo.InvariantCulture);
string output = dt.ToString(outputFormat, CultureInfo.InvariantCulture);

For example:

using System;
using System.Globalization;

class Test
{
    static void Main(string[] args)
    {
        string input = "10/13/2009 12:00:00 AM";
        string inputFormat = "MM/dd/yyyy HH:mm:ss tt";
        string outputFormat = "yyyyMMdd";
        DateTime dt = DateTime.ParseExact(input, inputFormat, 
                                          CultureInfo.InvariantCulture);
        string output = dt.ToString(outputFormat, CultureInfo.InvariantCulture);
        Console.WriteLine(output);
    }
}

Upvotes: 12

Related Questions