DateTime constant value

In this chapter you will learn:

  1. Create a DateTime with the current date and time
  2. Use the Now and UtcNow properties to get the currrent date and time

Now

using System;/*from   j a  va  2  s. c o m*/

class MainClass
{
    public static void Main(string[] args)
    {
        DateTime now = DateTime.Now;
        Console.WriteLine("Now   : {0}", now);
    }
}

The code above generates the following result.

UTC Now

using System;/*from j a v  a  2s.com*/

class MainClass
{

  public static void Main()
  {
    
    Console.WriteLine("DateTime.Now = " + DateTime.Now);
    Console.WriteLine("DateTime.UtcNow = " + DateTime.UtcNow);
    DateTime myDateTime = DateTime.Now;
    Console.WriteLine("myDateTime = " + myDateTime);
    DateTime myDateTime2 = DateTime.UtcNow;
    Console.WriteLine("myDateTime = " + myDateTime);

  }

}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Creating specific date/times: year, month, day, hour, minute, second
Home » C# Tutorial » Date, Time, TimeZone
Compare DateTimeOffset
DateTimeOffset creation
Date time Value from DateTimeOffset
Compare DateTime value
DateTime constant value
DateTime constructor
DateTime properties
Create TimeSpan From
TimeSpan
TimeSpan add and subtract
DateTime and TimeSpan
Compare TimeSpan
TimeSpan constant
TimeSpan Constructor
TimeSpan Duration
TimeSpan format
Negate a TimeSpan
TimeSpan operators
Parse string to TimeSpan
TimeSpan to string
TimeSpan total properties
TimeSpan propeties
Daylight saving
Time zone ID
UTC offset with TimeZone
TimeZone converting
TimeZone creation