Illustrates the use of DateTime and TimeSpan instances : Date Time « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » Date TimeScreenshots 
Illustrates the use of DateTime and TimeSpan instances
Illustrates the use of DateTime and TimeSpan instances

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example9_3.cs illustrates the use of DateTime and TimeSpan instances
*/

using System;

public class Example9_3
{

  public static void DisplayDateTime(
    string name, DateTime myDateTime
  )
  {

    Console.WriteLine(name + " = " + myDateTime);

    // display the DateTime's properties
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " +
      myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " +
      myDateTime.Ticks);

  }


  public static void Main()
  {

    // create a DateTime instance, specifying the year,
    // month, and day
    int year = 2002;
    int month = 12;
    int day = 25;
    DateTime myDateTime = new DateTime(year, month, day);

    // create a DateTime instance, specifying the year,
    // month, day, hour, minute, second, and millisecond
    int hour = 23;
    int minute = 30;
    int second = 12;
    int millisecond = 5;
    DateTime myDateTime2 =
      new DateTime(year, month, day, hour, minute, second, millisecond);

    // create a DateTime instance, specifying the year,
    // month, day, and JulianCalendar object
    System.Globalization.JulianCalendar myCalendar =
      new System.Globalization.JulianCalendar();
    DateTime myDateTime3 =
      new DateTime(year, month, day, myCalendar);

    // create a DateTime instance, specifying the number of ticks
    DateTime myDateTime4 = new DateTime(0);

    // display the various DateTime instances
    DisplayDateTime("myDateTime", myDateTime);
    DisplayDateTime("myDateTime2", myDateTime2);
    DisplayDateTime("myDateTime3", myDateTime3);
    DisplayDateTime("myDateTime4", myDateTime4);

    // create a TimeSpan instance, and add it to myDateTime4
    TimeSpan myTimeSpan = new TimeSpan(41210);
    myDateTime4 += myTimeSpan;
    DisplayDateTime("myDateTime4", myDateTime4);

  }

}


           
       
Related examples in the same category
1.Current date and time
2.Add 2 month to the date time
3.What day of the month is this?
4.Do some leap year checks
5.Look at the min and max date/time values
6.Output DateTime object
7.Add TimeSpan to DateTime
8.Constructors of DateTime
9.comparisons between DateTime objects
10.Parse and ParseExact
11.DateTime Now and its calculation
12.new DateTime(1900, 2, 29)
13.new DateTime(1900, 2, 29, new JulianCalendar())
14.Specify Kind DateTime
15.Offset of DateTime
16.DateTime and TimeSpan Instances
17.use the Now and UtcNow properties to get the currrent date and time
18.display the Date, Day, DayOfWeek, DayOfYear,Ticks, and TimeOfDayProperties of myDateTime
19.use the Compare() method to compare DateTime instances
20.use the overloaded less than operator (<) to compare two DateTime instances
21.use the Equals() method to compare DateTime instances
22.use the DaysInMonth() method to retrieve the number of days in a particular month and year
23.use the IsLeapYear() method to determine if a particular year is a leap year
24.use the Parse() method to convert strings to DateTime instances
25.use the Add() method to add a TimeSpan to a DateTime
26.use the Subtract() method to subtract a TimeSpan from a DateTime
27.use the overloaded addition operator (+) to add a TimeSpan to a DateTime
28.use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime
29.use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and AddSeconds() methods to add periods to a DateTime
30.Measures the time taken to add some numbersMeasures the time taken to add some numbers
31.Displays the words 'Hello World!' on the screen, along with the current date and timeDisplays the words 'Hello World!' on the screen, along with the current date and time
32.illustrates the use of TimeSpan properties and methodsillustrates the use of TimeSpan properties and methods
33.A simple clockA simple clock
34.Estimates pi by throwing points into a square. Use to compare execution timesEstimates pi by throwing points into a square. Use to               compare execution times
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.