C# - DateTime Class
Operations related to date and time in C# are done using the DateTime class.
E.g;
DateTime time = new DateTime( );
With the line, we can derive an object named time from the DateTime class.
In the example above, the time object was created in memory, but no value was assigned. It can also be used as follows if we want to assign a value at the same time while it is being created.
DateTime time = DateTime.Now;
In the above example, while the time object is created , the current date and time information is also assigned to the time object.
DateTime time = DateTime.Today;
Above, only the date information of today is assigned to the time object.
If we want to assign a date and time that we specify, we can do it as follows.
DateTime birthDate = new DateTime(1980, 8, 30, 8, 10, 12);
We gave year, month, day, hour, minute and second information respectively.
The following methods can be used to convert date and time information to text:
* In the examples, let's assume the value in the time object is Thursday, 21.05.2015 11:09:14 .
ToString( ) Method:
label1.Text = time.ToString();
The result to be written to the label when used in this way:
21.05.2015 11:09:14
The ToString method allows us to convert the information in time to string as it is and print it.
ToShortDateString( ) Method:
label1.Text = time.ToShortDateString();
The result when used this way:
21.05.2015
The ToShortDateString method allows us to convert only the date part of the information in time to string and print it.
ToLongDateString( ) Method:
label1.Text = time.ToLongDateString();
The result when used this way:
Thursday, 21.05.2015
The ToLongDateString method allows us to convert only the date part of the information in time to a string with the day name and print it.
ToShortTimeString( ) Method:
label1.Text = time.ToShortTimeString();
The result when used this way:
11:09
The ToShortTimeString method allows us to convert only the hour and minute part of the information in time to string and print it.
ToLongTimeString( ) Method:
label1.Text = time.ToLongTimeString();
The result when used this way:
11:09:14
The ToLongTimeString method allows us to convert only the hour, minute and second part of the information in time to string and print it.
Time and Date Operations
Working with times and dates in C# is quite easy. The ready-made methods used for these processes are:
Datetime.AddDays( ) Method: It is used to add days to the specified date. If a negative value is given as a parameter, it will subtract that many days.
datetime.AddHours( ) Method: Used to add hours.
datetime.AddMilliseconds( ) Method: It is used to add milliseconds.
datetime.AddMinutes( ) Method: Used to add minutes.
datetime.AddMonths( ) Method: Used to add month.
datetime.AddSeconds( ) Method: Used to add seconds.
Sample:
label1.Text = time.AddMinutes(25478).ToString( );
label1.Text = time.AddYears(23).ToShortDateString( );
label1.Text = time.AddHours(36).ToLongTimeString( );
Examples can be increased in a similar way for each method.
TimeSpan Class and Usage
TimeSpan literally means "time interval".
It belongs to the TimeSpan class;
We can get;
-
the total number of days with the TotalDays feature,
-
the total hours with the TotalHours feature,
-
the total minutes with the TotalMinutes feature,
-
the total seconds with the TotalSeconds feature,
-
the total seconds with the TotalMilliseconds feature.
Example: Let's calculate and print how much time has passed from our date of birth until now:
DateTime suAn = DateTime.Now;
DateTime birthDate = new DateTime(1980, 8, 30, 8, 10, 12);
TimeSpan elapsedTime = now - birthDate;
label1.Text = elapsedTime.ToString(); // Returns results in days, months, years, hours, minutes, seconds
label2.Text = elapsedTime.TotalDays.ToString(); // Returns the total number of days passed.
label3.Text = elapsedTime.TotalHours.ToString(); // Returns the total number of hours passed.
label4.Text = elapsedTime.TotalMinutes.ToString(); // Returns how many minutes have passed in total.
label5.Text = elapsedTime.TotalSeconds.ToString(); // Returns the total number of seconds.
c# datetime class, c# datetime operations, using datetime methods, timespan usage, date printing
EXERCISES
There are no examples related to this subject.
Read 847 times.