我需要删除日期时间的时间部分,或者可能使用以下格式的日期,格式object不是string.
06/26/2009 00:00:00:000
Run Code Online (Sandbox Code Playgroud)
我不能使用任何string转换方法,因为我需要object表单中的日期.
我尝试首先将其转换DateTime为a string,从中删除特定时间的日期,但是12:00:00 AM一旦我将其转换回来,它就会添加DateTime object.
dri*_*iis 775
使用Date属性:
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
Run Code Online (Sandbox Code Playgroud)
该date变量将包含日期,时间部分将00:00:00.
0x4*_*9D1 148
您可以使用格式字符串为输出字符串提供您喜欢的格式.
DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多.
Adr*_*lva 104
使用方法ToShortDateString.请参阅文档http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
var dateTimeNow = DateTime.Now; // Return 00/00/0000 00:00:00
var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000
Run Code Online (Sandbox Code Playgroud)
        Nic*_*ick 38
看看DateTime.Date属性.
获取此实例的日期组件.
sid*_*her 28
该Date物业将在午夜返回日期.
一种选择可以是单独获取单个值(日/月/年)并将其存储在您想要的类型中.
var dateAndTime = DateTime.Now; 
int year = dateAndTime.Year;
int month = dateAndTime.Month;
int day = dateAndTime.Day;
string.Format("{0}/{1}/{2}", month, day, year);
Run Code Online (Sandbox Code Playgroud)
        Meh*_*raD 19
上述答案都没有解决我在winforms上的问题.
达到ONLY日期最简单的方法是在Datetime中使用简单的函数:
DateTime dt = DateTime.now;
String BirthDate = dt.ToShortDateString();
Run Code Online (Sandbox Code Playgroud)
您只有生日字符串中的日期.
Ume*_*AKA 14
尝试为此制作自己的结构.DateTime对象将具有日期和时间
Tom*_*ter 14
你不能..NET中的DateTime始终有时间,默认为00:00:00:000.DateTime的Date属性也是DateTime(!),因此时间默认为00:00:00:000.
这是.NET Framework中的一个短缺,可以说.NET中的DateTime违反了单一责任原则.
L01*_*1NL 12
从 .NET 6 / C# 10 开始,您可以执行以下操作:
var dateOnly = DateOnly.FromDateTime(dateTime);
Run Code Online (Sandbox Code Playgroud)
        Vik*_*iaR 11
小智 11
最简单的方法是这样的,它只会返回日期:
var date = DateTime.Now.ToShortDateString();
Run Code Online (Sandbox Code Playgroud)
        sto*_*tom 10
这是另一种使用方法String.Format
    DateTime todaysDate = DateTime.UtcNow;
    string dateString = String.Format("{0:dd/MM/yyyy}", todaysDate);
    Console.WriteLine("Date with Time: "+ todaysDate.ToString());
    Console.WriteLine("Date Only : " + dateString);
Run Code Online (Sandbox Code Playgroud)
输出:
Date with Time: 9/4/2016 11:42:16 AM
Date Only : 04/09/2016
Run Code Online (Sandbox Code Playgroud)
如果日期时间存储在数据库中,这也适用.
有关更多日期和时间格式,请检查以下链接:
希望有所帮助
小智 7
这种方式只获得没有时间的日期
DateTime date = DateTime.Now;
string Strdateonly = date.ToString("d");
Run Code Online (Sandbox Code Playgroud)
输出= 2015年5月16日
Date向DateTime变量添加属性
var dateTime = DateTime.Now
var onlyDate = dateTime.Date
Run Code Online (Sandbox Code Playgroud)
DataType或者您也可以使用注释。
[DataType(DataType.Date)]
public DateTime dateTime {get; set;}
Run Code Online (Sandbox Code Playgroud)
DataType 注释位于System.ComponentModel.DataAnnotations命名空间内。
小智 5
我很惊讶没有人提到 DateTime.Today
var date = DateTime.Today;
// {7/1/2014 12:00:00 AM}
Run Code Online (Sandbox Code Playgroud)
见MSDN
小智 5
如果您要将其转换为字符串,您可以轻松地这样做。
我将日期作为您的 DateTime 对象。
date.ToString("d");
Run Code Online (Sandbox Code Playgroud)
这只会给你日期。
我写了一个DateOnly结构.这在皮肤下使用了DateTime,但没有时间部分公开暴露:
using System;
public struct DateOnly : IComparable, IFormattable, IComparable<DateOnly>, IEquatable<DateOnly>
{
    private DateTime _dateValue;
    public int CompareTo(object obj)
    {
        if (obj == null)
        {
            return 1;
        }
        DateOnly otherDateOnly = (DateOnly)obj;
        if (otherDateOnly != null)
        {
            return ToDateTime().CompareTo(otherDateOnly.ToDateTime());
        }
        else
        {
            throw new ArgumentException("Object is not a DateOnly");
        }
    }
    int IComparable<DateOnly>.CompareTo(DateOnly other)
    {
        return this.CompareToOfT(other);
    }
    public int CompareToOfT(DateOnly other)
    {
        // If other is not a valid object reference, this instance is greater.
        if (other == new DateOnly())
        {
            return 1;
        }
        return this.ToDateTime().CompareTo(other.ToDateTime());
    }
    bool IEquatable<DateOnly>.Equals(DateOnly other)
    {
        return this.EqualsOfT(other);
    }
    public bool EqualsOfT(DateOnly other)
    {
        if (other == new DateOnly())
        {
            return false;
        }
        if (this.Year == other.Year && this.Month == other.Month && this.Day == other.Day)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static DateOnly Now()
    {
        return new DateOnly(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
    }
    public static bool TryParse(string s, ref DateOnly result)
    {
        DateTime dateValue = default(DateTime);
        if (DateTime.TryParse(s, out dateValue))
        {
            result = new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
            return true;
        }
        else
        {
            return false;
        }
    }
    public static DateOnly Parse(string s)
    {
        DateTime dateValue = default(DateTime);
        dateValue = DateTime.Parse(s);
        return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
    }
    public static DateOnly ParseExact(string s, string format)
    {
        CultureInfo provider = CultureInfo.InvariantCulture;
        DateTime dateValue = default(DateTime);
        dateValue = DateTime.ParseExact(s, format, provider);
        return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
    }
    public DateOnly(int yearValue, int monthValue, int dayValue) : this()
    {
        Year = yearValue;
        Month = monthValue;
        Day = dayValue;
    }
    public DateOnly AddDays(double value)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddDays(value);
        return new DateOnly(d.Year, d.Month, d.Day);
    }
    public DateOnly AddMonths(int months)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddMonths(months);
        return new DateOnly(d.Year, d.Month, d.Day);
    }
    public DateOnly AddYears(int years)
    {
        DateTime d = new DateTime(this.Year, this.Month, this.Day);
        d = d.AddYears(years);
        return new DateOnly(d.Year, d.Month, d.Day);
    }
    public DayOfWeek DayOfWeek
    {
        get
        {
            return _dateValue.DayOfWeek;
        }
    }
    public DateTime ToDateTime()
    {
        return _dateValue;
    }
    public int Year
    {
        get
        {
            return _dateValue.Year;
        }
        set
        {
            _dateValue = new DateTime(value, Month, Day);
        }
    }
    public int Month
    {
        get
        {
            return _dateValue.Month;
        }
        set
        {
            _dateValue = new DateTime(Year, value, Day);
        }
    }
    public int Day
    {
        get
        {
            return _dateValue.Day;
        }
        set
        {
            _dateValue = new DateTime(Year, Month, value);
        }
    }
    public static bool operator == (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() == aDateOnly2.ToDateTime());
    }
    public static bool operator != (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() != aDateOnly2.ToDateTime());
    }
    public static bool operator > (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() > aDateOnly2.ToDateTime());
    }
    public static bool operator < (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() < aDateOnly2.ToDateTime());
    }
    public static bool operator >= (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() >= aDateOnly2.ToDateTime());
    }
    public static bool operator <= (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() <= aDateOnly2.ToDateTime());
    }
    public static TimeSpan operator - (DateOnly aDateOnly1, DateOnly aDateOnly2)
    {
        return (aDateOnly1.ToDateTime() - aDateOnly2.ToDateTime());
    }
    public override string ToString()
    {
        return _dateValue.ToShortDateString();
    }
    public string ToString(string format)
    {
        return _dateValue.ToString(format);
    }
    public string ToString(string fmt, IFormatProvider provider)
    {
        return string.Format("{0:" + fmt + "}", _dateValue);
    }
    public string ToShortDateString()
    {
        return _dateValue.ToShortDateString();
    }
    public string ToDbFormat()
    {
        return string.Format("{0:yyyy-MM-dd}", _dateValue);
    }
}
Run Code Online (Sandbox Code Playgroud)
这是从VB.NET转换而来,如果某些转换不是100%,请道歉
使用 date.ToShortDateString() 获取没有时间分量的日期
var date = DateTime.Now
var shortDate = date.ToShortDateString() //will give you 16/01/2019
Run Code Online (Sandbox Code Playgroud)
使用 date.ToString() 自定义日期格式
var date = DateTime.Now
var shortDate = date.ToString('dd-MMM-yyyy') //will give you 16-Jan-2019
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           739563 次  |  
        
|   最近记录:  |