How to get the current date without the time?

后端 未结 12 1579
青春惊慌失措
青春惊慌失措 2020-11-29 00:50

I am able to get date and time using:

DateTime now = DateTime.Now;

How can I get the current date and time separately in the DateTime forma

相关标签:
12条回答
  • 2020-11-29 01:17

    See, here you can get only date by passing a format string. You can get a different date format as per your requirement as given below for current date:

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

    Result : "9/1/2015"

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

    Result : "9-1-2015"

    DateTime.Now.ToString("yyyy-MM-dd");
    

    Result : "2015-09-01"

    DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
    

    Result : "2015-09-01 09:20:10"

    For more details take a look at MSDN reference for Custom Date and Time Format Strings

    0 讨论(0)
  • 2020-11-29 01:17

    I think you need separately date parts like (day, Month, Year)

    DateTime today = DateTime.Today;

    Will not work for your case. You can get date separately so you don't need variable today to be as a DateTimeType, so lets just give today variable int Type because the day is only int. So today is 10 March 2020 then the result of

    int today = DateTime.Today.Day;

    int month = DateTime.Today.Month;

    int year = DateTime.Today.Year;

    MessageBox.Show(today.ToString()+ " - this is day. "+month.ToString()+ " - this is month. " + year.ToString() + " - this is year");

    would be "10 - this is day. 3 - this is month. 2020 - this is year"

    0 讨论(0)
  • 2020-11-29 01:18

    for month

    DateTime.Now.ToString("MM");
    

    for day

    DateTime.Now.ToString("dd");
    

    for year

    DateTime.Now.ToString("yyyy");
    
    0 讨论(0)
  • 2020-11-29 01:20

    Use

    txtdate.Text = DateTime.Today.ToString("dd-MM-yyyy");
    
    0 讨论(0)
  • 2020-11-29 01:22

    You can use DateTime.Now.ToShortDateString() like so:

    var test = $"<b>Date of this report:</b> {DateTime.Now.ToShortDateString()}";
    
    0 讨论(0)
  • 2020-11-29 01:23

    Well, you can get just today's date as a DateTime using the Today property:

     DateTime today = DateTime.Today;
    

    or more generally, you can use the Date property. For example, if you wanted the UTC date you could use:

     DateTime dateTime = DateTime.UtcNow.Date;
    

    It's not very clear whether that's what you need or not though... if you're just looking to print the date, you can use:

    Console.WriteLine(dateTime.ToString("d"));
    

    or use an explicit format:

    Console.WriteLine(dateTime.ToString("dd/MM/yyyy"));
    

    See more about standard and custom date/time format strings. Depending on your situation you may also want to specify the culture.

    If you want a more expressive date/time API which allows you to talk about dates separately from times, you might want to look at the Noda Time project which I started. It's not ready for production just yet, but we'd love to hear what you'd like to do with it...

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