Date vs DateTime

后端 未结 12 1659
暖寄归人
暖寄归人 2020-11-30 05:13

I am working on a program that requires the date of an event to get returned.

I am looking for a Date, not a DateTime.

Is there

相关标签:
12条回答
  • 2020-11-30 05:42

    DateTime has a Date property that you can use to isolate the date part. The ToString method also does a good job of only displaying the Date part when the time part is empty.

    0 讨论(0)
  • 2020-11-30 05:50

    The DateTime object has a Property which returns only the date portion of the value.

        public static void Main()
    {
        System.DateTime _Now = DateAndTime.Now;
        Console.WriteLine("The Date and Time is " + _Now);
        //will return the date and time
        Console.WriteLine("The Date Only is " + _Now.Date);
        //will return only the date
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
    
    0 讨论(0)
  • 2020-11-30 05:58

    There is no Date DataType.

    However you can use DateTime.Date to get just the Date.

    E.G.

    DateTime date = DateTime.Now.Date;
    
    0 讨论(0)
  • 2020-11-30 05:59

    For this, you need to use the date, but ignore the time value.

    Ordinarily a date would be a DateTime with time of 00:00:00

    The DateTime type has a .Date property which returns the DateTime with the time value set as above.

    0 讨论(0)
  • 2020-11-30 06:00

    I created a simple Date struct for times when you need a simple date without worrying about time portion, timezones, local vs. utc, etc.

    Date today = Date.Today;
    Date yesterday = Date.Today.AddDays(-1);
    Date independenceDay = Date.Parse("2013-07-04");
    
    independenceDay.ToLongString();    // "Thursday, July 4, 2013"
    independenceDay.ToShortString();   // "7/4/2013"
    independenceDay.ToString();        // "7/4/2013"
    independenceDay.ToString("s");     // "2013-07-04"
    int july = independenceDay.Month;  // 7
    

    https://github.com/claycephus/csharp-date

    0 讨论(0)
  • 2020-11-30 06:01

    Unfortunately, not in the .Net BCL. Dates are usually represented as a DateTime object with the time set to midnight.

    As you can guess, this means that you have all the attendant timezone issues around it, even though for a Date object you'd want absolutely no timezone handling.

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