How to change time in DateTime?

2019-01-07 02:50发布

How can I change only the time in my DateTime variable "s"?

DateTime s = some datetime;

标签: c# datetime time
27条回答
Root(大扎)
2楼-- · 2019-01-07 03:19

Alright I'm diving in with my suggestion, an extension method:

public static DateTime ChangeTime(this DateTime dateTime, int hours, int minutes, int seconds, int milliseconds)
{
    return new DateTime(
        dateTime.Year,
        dateTime.Month,
        dateTime.Day,
        hours,
        minutes,
        seconds,
        milliseconds,
        dateTime.Kind);
}

Then call:

DateTime myDate = DateTime.Now.ChangeTime(10,10,10,0);

It's important to note that this extension returns a new date object, so you can't do this:

DateTime myDate = DateTime.Now;
myDate.ChangeTime(10,10,10,0);

But you can do this:

DateTime myDate = DateTime.Now;
myDate = myDate.ChangeTime(10,10,10,0);
查看更多
迷人小祖宗
3楼-- · 2019-01-07 03:19

Since DateTime is immutable, a new instance has to be created when a date component needs to be changed. Unfortunately, there is no built-in functionality to set individual components of a DateTime instance.

Using the following extension methods

public static DateTime SetPart(this DateTime dateTime, int? year, int? month, int? day, int? hour, int? minute, int? second)
{
    return new DateTime(
        year ?? dateTime.Year,
        month ?? dateTime.Month,
        day ?? dateTime.Day,
        hour ?? dateTime.Hour,
        minute ?? dateTime.Minute,
        second ?? dateTime.Second
    );
}

public static DateTime SetYear(this DateTime dateTime, int year)
{
    return dateTime.SetPart(year, null, null, null, null, null);
}

public static DateTime SetMonth(this DateTime dateTime, int month)
{
    return dateTime.SetPart(null, month, null, null, null, null);
}

public static DateTime SetDay(this DateTime dateTime, int day)
{
    return dateTime.SetPart(null, null, day, null, null, null);
}

public static DateTime SetHour(this DateTime dateTime, int hour)
{
    return dateTime.SetPart(null, null, null, hour, null, null);
}

public static DateTime SetMinute(this DateTime dateTime, int minute)
{
    return dateTime.SetPart(null, null, null, null, minute, null);
}

public static DateTime SetSecond(this DateTime dateTime, int second)
{
    return dateTime.SetPart(null, null, null, null, null, second);
}

you can set individual DateTime components like

var now = DateTime.Now;

now.SetSecond(0);
查看更多
欢心
4楼-- · 2019-01-07 03:20

What's wrong with DateTime.AddSeconds method where you can add or substract seconds?

查看更多
forever°为你锁心
5楼-- · 2019-01-07 03:21

I prefer this:

DateTime s = //get some datetime;
s = new DateTime(s.Year, s.Month,s.Day,s.Hour,s.Minute,0);
查看更多
相关推荐>>
6楼-- · 2019-01-07 03:23
 Using an extencion to DateTime:  

        public enum eTimeFragment
        {
            hours,
            minutes,
            seconds,
            milliseconds
        }
        public static DateTime ClearTimeFrom(this DateTime dateToClear, eTimeFragment etf)
        {
            DateTime dtRet = dateToClear;
            switch (etf)
            {
                case eTimeFragment.hours:
                    dtRet = dateToClear.Date;
                    break;
                case eTimeFragment.minutes:
                    dtRet = dateToClear.AddMinutes(dateToClear.Minute * -1);
                    dtRet = dtRet.ClearTimeFrom(eTimeFragment.seconds);
                    break;
                case eTimeFragment.seconds:
                    dtRet = dateToClear.AddSeconds(dateToClear.Second * -1);
                    dtRet = dtRet.ClearTimeFrom(eTimeFragment.milliseconds);
                    break;
                case eTimeFragment.milliseconds:
                    dtRet = dateToClear.AddMilliseconds(dateToClear.Millisecond * -1);
                    break;
            }
            return dtRet;

        }

Use like this:

Console.WriteLine (DateTime.Now.ClearTimeFrom(eTimeFragment.hours))

this has to return: 2016-06-06 00:00:00.000

查看更多
ら.Afraid
7楼-- · 2019-01-07 03:23

here is a ghetto way, but it works :)

DateTime dt = DateTime.Now; //get a DateTime variable for the example
string newSecondsValue = "00";
dt = Convert.ToDateTime(dt.ToString("MM/dd/yyyy hh:mm:" + newSecondsValue));
查看更多
登录 后发表回答