开发者

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 YYYYMMD开发者_JAVA百科D format using c#


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


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜