Home  >  Article  >  Backend Development  >  Time functions in C#

Time functions in C#

WBOY
WBOYforward
2023-09-15 21:37:02684browse

C# 中的时间函数

DateTime has methods and properties for date and time, and how to get the hours or minutes of a day, etc.

Let's just focus on the time function -

Please refer to MSDN (Microsoft Developer Network) for all functions -

##1 23456
Sr.No. Methods and Properties
AddDays(Double) Returns a new DateTime that adds the specified number of days to this instance's value.

AddHours(Double)Returns a new DateTime that will specify The number of hours added to this instance's value.

AddMilliseconds(Double)Returns the number of new DateTime milliseconds added to the specified number is the value of this instance.

AddMinutes(Double)Returns a new DateTime that will specify The number of minutes added to this instance's value.

AddSeconds(Double)Returns a new DateTime that will specify The number of seconds to add to this instance's value.

AddYears(Int32) Returns a new DateTime, adding the specified Number of years is added to the value

Let us understand a Time function i.e. AddMilliseconds(Double) which adds the specified number of milliseconds to the value of this instance .

Example

Real-time demonstration

using System;

public class Demo {
   public static void Main() {
      string dateFormat = "MM/dd/yyyy hh:mm:ss.fffffff";
      DateTime dateCurrent = new DateTime(2018, 7, 23, 13, 0, 0);
      Console.WriteLine("Original date: {0} ({1:N0} ticks)", dateCurrent.ToString(dateFormat),          dateCurrent.Ticks);

      DateTime dateNew = dateCurrent.AddMilliseconds(1);
      Console.WriteLine("Next date: {0} ({1:N0} ticks)", dateNew.ToString(dateFormat),          dateNew.Ticks);

   }
}

Output

Original date: 07/23/2018 01:00:00.0000000 (636,679,476,000,000,000 ticks)
Next date: 07/23/2018 01:00:00.0010000 (636,679,476,000,010,000 ticks)

The above is the detailed content of Time functions in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete