How do I get today’s date in C# in mm/dd/yyyy format ?

How do I get today’s date in C# in mm/dd/yyyy format ?

Asked on December 20, 2018 in C#.
Add Comment


  • 7 Answer(s)

    To get today’s date in  mm/dd/yyyy format is very easy by use the Date.Time() in c#.

    DateTime.Now.ToString("M/d/yyyy");
    

    For more reference : 

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

     
    Answered on December 20, 2018.
    Add Comment

    This code is used to  convert the date format. It should be better.

    using System.Globalization;
    using System.Threading;
     
    ...
     
    var currentCulture = Thread.CurrentThread.CurrentCulture;
    try {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-us");
      string shortDateString = DateTime.Now.ToShortDateString();
      // Do something with shortDateString...
    } finally {
      Thread.CurrentThread.CurrentCulture = currentCulture;
    }
    
    Answered on December 20, 2018.
    Add Comment

    You can achieve this using by a simple,

    DateTime.Now.ToString("dd/MM/yyyy");
    
    Answered on December 20, 2018.
    Add Comment

    You should use DateTime.Today:

    DateTime today = DateTime.Today; // As DateTime
    string s_today = today.ToString("MM/dd/yyyy"); // As String

    Edit: You edited your post to add another question, so here comes my edit to supply at least some sort of answer.

    Update While you can use DateTime.Compare() you should use plain comparisson:

    if(today < otherdate)
    {
        // Do something.
    }

    Alternatively, you can use DateTime-variables to check against other DateTime-variables using the DateTime.Compare() method. Both otpions will work and it comes down to preference and what you want to do with the result.

    int result = DateTime.Compare(today, otherdate);
    
    if(result < 0)
        MessageBox.Show("Today is earlier than the 'otherdate'");
    elseif(result > 0)
        MessageBox.Show("Today is later than the 'other date'");
    else
        MessageBox.Show("Dates are equal...");
    Answered on January 15, 2019.
    Add Comment

    To convert DateTime variable to string in the specified format:

    DateTime d = ...;
    string s = d.ToString("MM/dd/yyyy");

    If you want to compare only date part of DateTime, not time part:

    DateTime d1 = DateTime.Parse("10/10/2011");
    DateTime d2 = DateTime.Parse("01/01/2011");
    
    if (d1.Date > d2.Date)
    {
       // do the stuff
    }
    Answered on January 15, 2019.
    Add Comment

     

    DateTime.Now.ToString("MM/dd/yyyy"); 
    DateTime.Today.ToString("MM/dd/yyyy"); 
    Answered on January 28, 2019.
    Add Comment
    DateTime.Now.ToString("MM/dd/yyyy"); 
    DateTime.Today.ToString("MM/dd/yyyy"); 
    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.