c#输出当前日期和当前时间
To add hours in the current date-time, we use AddHours() method of DateTime class in C#.
要在当前日期时间添加小时数 ,我们在C#中使用DateTime类的AddHours()方法。
Syntax:
句法:
DateTime DateTime.AddHours(double);
AddHours() method accepts the value of the hours as double and returns the DateTime object that can be parsed and updated date time can be found.
AddHours()方法接受小时值的两倍,并返回可以解析的DateTime对象,并可以找到更新的日期时间。
C# code to add hours in current date time
C#代码在当前日期时间添加小时数
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//creating an object of DateTime class
//and, initializing it with the current time
//using "Now"
DateTime dt1 = DateTime.Now;
//printing current date time
Console.WriteLine("Currnt date time is: " + dt1.ToString());
//another DateTime object to store the updated date time
//adding 30 hours
DateTime dt2 = dt1.AddHours(30);
//updated date time is
Console.WriteLine("Updated date time is: " + dt2.ToString());
//adding 48 hours (i.e. 2 days) in the time
dt2 = dt1.AddHours(48);
//updated date time is
Console.WriteLine("Updated date time is: " + dt2.ToString());
//just to print a new line
Console.WriteLine();
}
}
}
Output
输出量
RUN 1:
Currnt date time is: 10/17/2019 5:33:04 PM
Updated date time is: 10/18/2019 11:33:04 PM
Updated date time is: 10/19/2019 5:33:04 PM
RUN 2:
Currnt date time is: 10/17/2019 5:35:53 PM
Updated date time is: 10/18/2019 11:35:53 PM
Updated date time is: 10/19/2019 5:35:53 PM
翻译自: https://www.includehelp.com/dot-net/add-hours-in-the-current-date-time-in-csharp.aspx
c#输出当前日期和当前时间