Converting string format to datetime in mm/dd/yyyy

前端 未结 6 1049
萌比男神i
萌比男神i 2020-11-27 18:39

I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format.

string strDate = DateTime.Now.ToString(\"MM/dd/         


        
相关标签:
6条回答
  • 2020-11-27 19:16

    The following works for me.

    string strToday = DateTime.Today.ToString("MM/dd/yyyy");
    
    0 讨论(0)
  • 2020-11-27 19:18

    I did like this

    var datetoEnter= DateTime.ParseExact(createdDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-11-27 19:25

    You can change the format too by doing this

    string fecha = DateTime.Now.ToString(format:"dd-MM-yyyy");
    

    // this change the "/" for the "-"

    0 讨论(0)
  • 2020-11-27 19:30

    Solution

    DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
    
    0 讨论(0)
  • 2020-11-27 19:34

    You need an uppercase M for the month part.

    string strDate = DateTime.Now.ToString("MM/dd/yyyy");
    

    Lowercase m is for outputting (and parsing) a minute (such as h:mm).

    e.g. a full date time string might look like this:

    string strDate = DateTime.Now.ToString("MM/dd/yyyy h:mm");
    

    Notice the uppercase/lowercase mM difference.


    Also if you will always deal with the same datetime format string, you can make it easier by writing them as C# extension methods.

    public static class DateTimeMyFormatExtensions
    {
      public static string ToMyFormatString(this DateTime dt)
      {
        return dt.ToString("MM/dd/yyyy");
      }
    }
    
    public static class StringMyDateTimeFormatExtension
    {
      public static DateTime ParseMyFormatDateTime(this string s)
      {
        var culture = System.Globalization.CultureInfo.CurrentCulture;
        return DateTime.ParseExact(s, "MM/dd/yyyy", culture);
      }
    }
    

    EXAMPLE: Translating between DateTime/string

    DateTime now = DateTime.Now;
    
    string strNow = now.ToMyFormatString();
    DateTime nowAgain = strNow.ParseMyFormatDateTime();
    

    Note that there is NO way to store a custom DateTime format information to use as default as in .NET most string formatting depends on the currently set culture, i.e.

    System.Globalization.CultureInfo.CurrentCulture.
    

    The only easy way you can do is to roll a custom extension method.

    Also, the other easy way would be to use a different "container" or "wrapper" class for your DateTime, i.e. some special class with explicit operator defined that automatically translates to and from DateTime/string. But that is dangerous territory.

    0 讨论(0)
  • 2020-11-27 19:38

    You are looking for the DateTime.Parse() method (MSDN Article)

    So you can do:

    var dateTime = DateTime.Parse("01/01/2001");
    

    Which will give you a DateTime typed object.

    If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)

    Which you would use in a situation like this (Where you are using a British style date format):

    string[] formats= { "dd/MM/yyyy" }
    var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
    
    0 讨论(0)
提交回复
热议问题