Come ottenere solo la parte temporale da DateTime in C#

Come ottenere solo la parte temporale da DateTime in C#

In questo articolo impareremo come ottenere l'unica parte temporale da DateTime in C#. useremo il DateTime oggetto per inizializzare la data con l'ora. E usa il ToString() metodo per formattare il DateTime per avere il tempo. In questo programma, utilizzeremo gli specificatori del formato dell'ora per ottenere l'ora solo dal Datetime oggetto.

Ecco il codice sorgente del programma per ottenere solo la parte temporale da DateTime in C#

Come ottenere solo la parte di tempo da DateTime in C#
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();
        }
    }
}
Uscita

Ora:15:50:00

Ora:15:50

Ora:15:50:00

Ora:15:50:00

Ora:15:50

Ora:15:50:00

Formato:HH:mm tt e l'ora è:15:50

Formato:HH:mm:ss tt e l'ora è:15:50:00

Formato:HH:mm:ss:ffff tt e l'ora è:15:50:00:0000 PM

Formato:HH:mm:ss e Ora è:15:50:00

Formato:HH:mm e Ora:15:50

Formato:hh:mm tt e l'ora è:15:50

Formato:hh:mm:ss tt e l'ora è:15:50:00

Formato:hh:mm:ss:ffffff tt e l'ora è:03:50:00:0000000 PM

Formato:hh:mm:ss e Ora è:03:50:00

Formato:hh:mm e l'ora è:03:50

Spero che questo articolo ti aiuti a capire come ottenere l'unica parte temporale da DateTime in C#.

Condividi il tuo prezioso feedback, per favore pubblica il tuo commento in fondo a questo articolo. Grazie!