Convert string to specific DateTime format

前端 未结 5 1555
既然无缘
既然无缘 2021-01-07 01:17

I\'ve been googling for a while now and for the life of me can\'t seem to find a solution. I thought this would be easy but it\'s taking too long and am turning to stackover

相关标签:
5条回答
  • 2021-01-07 02:02

    Try this.

        DateTime dt;
        if (DateTime.TryParse(startTime, out dt))
        {
            string newDateTime = dt.ToString("MM/dd/yyyy hh:mmtt");
        }
    
    0 讨论(0)
  • 2021-01-07 02:05

    Here is example for this.

        String strDate="12/20/2013";
        string strFormat="dd/MM/yyyy";
        DateTime objDT;
    
        if (DateTime.TryParseExact(strDate, strFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out objDT) == true)
        {
            Response.Write("<b>Formatted DateTime : </b>" + objDT.ToString());
        }
        else
        {
            Response.Write("<b>Not able to parse datetime.</b>");
        }
    
    0 讨论(0)
  • 2021-01-07 02:06

    I think that when you say it's adding seconds, you mean when you try to read it out it's reading the seconds as well, you want to do something like

    DateTime parsedDateTime = DateTime.ParseExact(startTime, "MM/DD/YYYY hh:mmtt", null);
    string dateTime = parseDateTime.ToString("MM/DD/YYY hh:mmtt");
    

    DateTime class always has seconds, and milliseconds, and nanoseconds I believe stored in it. Always, Whether you fill them in or not it assumes the, though you just have to format how you read the info from the DateTime object to make it not give you the pieces of DateTime data which you didn't fill in or are not using.

    If you're storing it in a database, understand the database will also store all of this information regardless of whether or not you give it to it. There are some different SQL DateTime types like SmallDate and etc which store varying amounts of DateTime data, don't know if there's one that stores to the hour and not seconds as well, but with SQL you can also format the way you select the data out, or just select it out into your C# as a C# DateTime object, and then when you represent it again, format the ToString() so it does not present the data you don't want/have.

    0 讨论(0)
  • 2021-01-07 02:18

    If you have a string, you must first guarantee that it can indeed be converted to a datetime.
    So first, do so. Once you have a datetime, store it in a database as is, you do not need to be copncerned about format, (as others have said here).

    and then, if you need to, reformat it to a different representation only if you need to display it somewhere..

    string s = "21 June 2010 09:23:56";
    DateTime dt;
    if (DateTime.TryParse(s, out dt))
       // dit is a valid datetime, storeit or whatever
    else
       throw ApplicationException(
            "{0} is not a valid datetime.", s);
    //  --  and now you can redisplay dt any way you want -----
    debug.Print(s.ToString("d MMM yyyy");
    debug.Print(s.ToString("HH:mm:ss");
    debug.Print(s.ToString("ffffdd, d MMM yy");
    debug.Print(s.ToString("MMM d, yyyy");
    debug.Print(s.ToString("HH:mm:ss ffffdd");
    

    // etc...

    0 讨论(0)
  • 2021-01-07 02:23

    If it's stored as a DateTime data type, it's stored correctly, but your UI is displaying it wrong. The DateTime data type always has the seconds (milliseconds, etc) regardless of how you set the value. The problem is in how it's being displayed back to the user.

    You need to display the date you want in the right string format at display time as in

    Label1.Text = startTime.ToString("MM/dd/yyyy hh:mmtt");
    

    Edit - added

    To format it in a GridView, see here: http://peterkellner.net/2006/05/24/how-to-set-a-date-format-in-gridview-using-aspnet-20using-htmlencode-property/

    0 讨论(0)
提交回复
热议问题