如何设置日期时间变量在C#中只有一部分时间 [英] How to set only time part of a DateTime variable in C#

查看:287
本文介绍了如何设置日期时间变量在C#中只有一部分时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期时间变量:

DateTime date = DateTime.Now;

我想换一个DateTime变量的时间的一部分。但是,当我试图访问时间部分(HH:MM:SS),这些字段是只读

I want to change the time part of a DateTime variable. But when I tried to access time part (hh:mm:ss) these fields are readonly.

我不能设置这些属性?

推荐答案

使用,它允许您指定的年,月,日,时,分,秒的构造器:

Use the constructor that allows you to specify the year, month, day, hours, minutes, and seconds:

var dateNow = DateTime.Now;
var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 4, 5, 6);

这篇关于如何设置日期时间变量在C#中只有一部分时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆