SoFunction
Updated on 2025-03-07

Time display format in C# (12-hour system vs 24-hour system)

C# time display format

Let's take a look together:

24-hour system

this. = “Hello,WelcomeXXXXControl system!” + “Current time:” + (“yyyy-MM-dd HH:mm:ss”);

12-hour system

this. = “Hello,WelcomeXXXXControl system!” + “Current time:” + (“yyyy-MM-dd hh:mm:ss”);

Attached system time display method

Step 1: Add [StatusStrip]-[StatusLabel] at the bottom of the form;

Step 2: Add a timer, get the current system time through the timer, and update it according to its Interval machine. Double-click the timer to add events

It should be noted that the timer needs to be turned on [True]

Attach the code as follows

 private void timer1_Tick(object sender, EventArgs e)
 {
     this. = "Hello, welcome to the nuclear target processing control system!" + "Current time:" + ("yyyy-MM-dd HH:mm:ss");
 }

C# gets the current time and processing time (adds and subtracts the time)

1. Get the current time and format the time into a string

  DateTime dt=;
  string str=();        //This is the most direct conversion method  string str2=("yyy-MM-dd HH:mm:ss");

2. Obtain year, month, day, hour, minute, second, etc.

  DateTime dt=;  //Get the current time  int y=;      //Year  int m=;     //moon  int d=;       //day  int h=;      //hour  int n=;    //point  int s=;    //Second  int ms=;     //millisecond  long t=;      //A number,Used to indicate the time,The note type islong

3. Time comparison

DateTime dt1=;
DateTime dt2= (3);          //Time plus 3 yearsint ct1 =(dt2);                 //dt1 is earlier than dt2, returns -1;int ct2 =(dt1);                  //dt2 is later than dt1, return 1;int ct3=(dt1);             //dt2 is equal to dt1, return 0;DateTime dt3 =(4);             //Time plus 4 yearsbool b =Equals(dt2);                          //dt1anddt2Not equal,returnfalse

4. Time addition and subtraction

 DateTime dt= ;
 dt =(1);              //Add 1 year dt =(1);               //Add 1 month dt =(13);              //Add 13 days dt =(1);                   //Add 1 hour dt =(1);            //Add 1 minute dt =(1);              //  Add 1 second dt= (1);         // Add 1 millisecond dt  =();              //addTickettime,用数字表示当前time

The parameter is positive and indicates addition, and the parameter is negative and indicates subtraction. Pay attention to the spelling. The function should return a value, for example: dt=(1), not (1).

5. Time minus operation

The previous time addition and subtraction are adding and subtracting a time. Here, the two times are subtracted (using -), and the returned result type is TimeSpan.

DateTime dt1 =;
DateTime dt2 =(3);
TimeSpan ts =dt1-dt2;

The properties of TimeSpan Days, Hours, Minutes, Seconds, MillSeconds and Tickets return the difference between days, hours, fractions, seconds, milliseconds and Tickets, respectively, which are positive and negative.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.