Home » C#

Get milliseconds of the current time in C#

C# DateTime class example: Here, we are going to learn how to get the milliseconds only of the current time in C#?
Submitted by IncludeHelp, on October 18, 2019

To get the milliseconds only of the current time, we use the "Millisecond" property of the DateTime class in C#. We use the "Millisecond" property with the object of DateTime class which should be initialized with the current date-time i.e. "Now". The property returns an integer value that is the milliseconds value of the current time.

Syntax:

    int DateTime.Millisecond;

This property returns an integer value.

C# code to get milliseconds only from the current time

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating an object of DateTime class
            //and, initializing it with the current time 
            //using "Now"
            DateTime dt = DateTime.Now;

            //getting Milliseconds only from the currenttime
            int ms = dt.Millisecond;

            //printing the current date & time
            Console.WriteLine("The current time is: " + dt.ToString());

            //printing the Milliseconds value of the current time
            Console.WriteLine("Milliseconds of current time: " + ms.ToString());

            //just to print a new line 
            Console.WriteLine();
        }
    }
}

Output

RUN 1:
The current time is: 10/17/2019 1:09:19 PM
Milliseconds of current time: 742

RUN 2:
The current time is: 10/17/2019 1:10:23 PM
Milliseconds of current time: 781





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.