So erhalten Sie nur einen Zeitteil von DateTime in C#

So erhalten Sie nur einen Zeitteil von DateTime in C#

In diesem Artikel erfahren Sie, wie Sie den einzigen Zeitteil von DateTime in C# erhalten. wir werden den DateTime verwenden Objekt, um das Datum mit der Uhrzeit zu initialisieren. Und verwenden Sie die ToString() Methode zum Formatieren von DateTime um die Zeit zu bekommen. In diesem Programm verwenden wir Zeitformatbezeichner, um die Zeit nur aus Datetime abzurufen Objekt.

Hier ist der Quellcode des Programms, um nur den Zeitteil von DateTime in C# zu erhalten

So erhalten Sie in C# nur einen Teil der Uhrzeit aus Datetime
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Get Only Time Part From Datetime in C# */
        static void Main(string[] args)
        {
            //initialize a datetime variable
            DateTime date = new DateTime(2019, 10, 12, 15, 50, 00);

            //Using TimeSpan
            TimeSpan TodayTime = date.TimeOfDay;

            Console.WriteLine("Time: {0}", TodayTime);
            //Output ==> Time: 15:50:00

            Console.WriteLine("Time: {0}", date.ToString("t"));
            //Output ==> Time: 15:50

            Console.WriteLine("Time: {0}", date.ToString("T"));
            //Output ==> Time: 15:50:00

            Console.WriteLine("Time: {0}", date.ToLongTimeString());
            //Output ==> Time: 15:50:00

            Console.WriteLine("Time: {0}", date.ToShortTimeString());
            //Output ==> Time: 15:50

            Console.WriteLine("Time: {0}", date.GetDateTimeFormats('T')[0]);
            //Output ==> Time: 15:50:00

            Console.WriteLine("Format: HH:mm tt and Time is: {0}", date.ToString("HH:mm tt"));
            //Output ==> Format: HH:mm tt and Time is: 15:50 PM

            Console.WriteLine("Format: HH:mm:ss tt and Time is: {0}", date.ToString("HH:mm:ss tt"));
            //Output ==> Format: HH:mm:ss tt and Time is: 15:50:00 PM

            Console.WriteLine("Format: HH:mm:ss:ffff tt and Time is: {0}", date.ToString("HH:mm:ss:ffff tt"));
            //Output ==> Format: HH:mm:ss:ffff tt and Time is: 15:50:00:0000 PM

            Console.WriteLine("Format: HH:mm:ss and Time is: {0}", date.ToString("HH:mm:ss"));
            //Output ==> Format: HH:mm:ss and Time is: 15:50:00

            Console.WriteLine("Format: HH:mm and Time is: {0}", date.ToString("HH:mm"));
            //Output ==> Format: HH:mm and Time is: 15:50

            Console.WriteLine("Format: hh:mm tt and Time is: {0}", date.ToString("hh:mm tt"));
            //Output ==> Format: hh:mm tt and Time is: 03:50 PM

            Console.WriteLine("Format: hh:mm:ss tt and Time is: {0}", date.ToString("hh:mm:ss tt"));
            //Output ==> Format: hh:mm:ss tt and Time is: 03:50:00 PM

            Console.WriteLine("Format: hh:mm:ss:fffffff tt and Time is: {0}", date.ToString("hh:mm:ss:fffffff tt"));
            //Output ==> Format: hh:mm:ss:fffffff tt and Time is: 03:50:00:0000000 PM

            Console.WriteLine("Format: hh:mm:ss and Time is: {0}", date.ToString("hh:mm:ss"));
            //Output ==> Format: hh:mm:ss and Time is: 03:50:00

            Console.WriteLine("Format: hh:mm and Time is: {0}", date.ToString("hh:mm"));
            //Output ==> Format: hh:mm and Time is: 03:50

            Console.ReadKey();
        }
    }
}
Ausgabe

Uhrzeit:15:50:00

Zeit:15:50

Uhrzeit:15:50:00

Uhrzeit:15:50:00

Zeit:15:50

Uhrzeit:15:50:00

Format:HH:mm tt und Uhrzeit:15:50 Uhr

Format:HH:mm:ss tt und Uhrzeit:15:50:00 Uhr

Format:HH:mm:ss:ffff tt und Uhrzeit:15:50:00:0000

Format:HH:mm:ss und Uhrzeit:15:50:00

Format:HH:mm und Uhrzeit:15:50

Format:hh:mm tt und Uhrzeit:15:50 Uhr

Format:hh:mm:ss tt und Uhrzeit:15:50:00 Uhr

Format:hh:mm:ss:fffffff tt und Uhrzeit:03:50:00:0000000 PM

Format:hh:mm:ss und Uhrzeit:03:50:00

Format:hh:mm und Uhrzeit:03:50

Ich hoffe, dieser Artikel hilft Ihnen zu verstehen, wie Sie den einzigen Zeitteil von DateTime in C# erhalten.

Teilen Sie Ihr wertvolles Feedback, bitte posten Sie Ihren Kommentar am Ende dieses Artikels. Vielen Dank!