Sponsored Ad

Thursday, October 22, 2009

C# DateTime.Now

The syntax used to DateTime.Now is a static property. This means you do not need to call an instance of the DateTime structure. Also, do not use it with parentheses or parameters. This example uses DateTime.Now, and saves it as a property in a class.

Program that uses DateTime.Now (C#)

using System;

class Program
{
    class Employee
    {
        public DateTime HiringDate { get; set; }
    }

    static void Main()
    {
        //
        // Write the current date and time.
        //
        DateTime now = DateTime.Now;
        Console.WriteLine(now);

        //
        // Store a DateTime in a class.
        //
        Employee employee = new Employee() { HiringDate = now };
        Console.WriteLine(employee.HiringDate);
    }
}

Output of the program

10/9/2009 9:45:06 PM

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers