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条回答
Lonely孤独者°
2楼-- · 2019-01-07 03:25

In fact, you can't change the time once it's created. But you can create it easily with many constructors: https://docs.microsoft.com/en-us/dotnet/api/system.datetime.-ctor?view=netframework-4.7.2

For example, if you want to create a DateTime changing Seconds, you can just do this:

DateTime now = DateTime.Now;
DateTime secondschanged = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, yourseconds);
查看更多
祖国的老花朵
3楼-- · 2019-01-07 03:27

If you have a DateTime like 2014/02/05 18:19:51 and want just 2014/02/05, you can do that:

_yourDateTime = new DateTime(_yourDateTime.Year, _yourDateTime.Month, _yourDateTime.Day)
查看更多
放我归山
4楼-- · 2019-01-07 03:30

When you construct your DateTime object, use a constructor that allows you to specify time:

var myDateTime = new DateTime(2000, 01, 01, 13, 37, 42);  // 2000-01-01 13:37:42

If you already have a DateTime object and wish to change the time, uou can add minutes, hours or seconds to your DateTime using simple methods:

var myDateTime = new DateTime(2000, 01, 01);              // 2000-01-01 00:00:00
myDateTime = myDateTime.AddHours(13);                     // 2000-01-01 13:00:00
myDateTime = myDateTime.AddMinutes(37);                   // 2000-01-01 13:37:00
myDateTime = myDateTime.AddSecounds(42);                  // 2000-01-01 13:37:42

Notice how we have to "save" the result from each method call to the myDateTime variable. This is because the DateTime is immutable, and its methods simply create new instances with the extra hours/minutes/seconds added.

If you need to add both hours and minutes (and/or seconds) and the same time, you can simplify the code by adding a TimeSpan to the original DateTime instead:

var myDateTime = new DateTime(2000, 01, 01);              // 2000-01-01 00:00:00
myDateTime += new TimeSpan(13, 37, 42);                   // 2000-01-01 13:37:42

If you want to set absolute hours/minues/seconds, rather than adding to the existing values, you can use the aforementioned DateTime constructor, and reuse values for year/month/day from earlier:

myDateTime = new DateTime(myDateTime.Year, myDateTime.Month, myDateTime.Day,
                          20, 33, 19)                     // 2000-01-01 20:33:19
查看更多
对你真心纯属浪费
5楼-- · 2019-01-07 03:30

The best solution is:

currdate.AddMilliseconds(currdate.Millisecond * -1).AddSeconds(currdate.Second * -1).AddMinutes(currdate.Minute * -1).AddHours(currdate.Hour * -1);
查看更多
贼婆χ
6楼-- · 2019-01-07 03:31

Use Date.Add and add a New TimeSpan with the new time you want to add

DateTime dt = DateTime.Now
dt.Date.Add(new TimeSpan(12,15,00))
查看更多
对你真心纯属浪费
7楼-- · 2019-01-07 03:32
  DateTime s;
//s = datevalue
                s = s.AddMilliseconds(10);
                s = s.AddMinutes(10);
                s = s.AddSeconds(10);
                s = s.AddHours(10);

you could add +ve/-ve values in parameter.

s.Add(new TimeSpan(1, 1, 1));
查看更多
登录 后发表回答