illustrates the use of TimeSpan properties and methods : Date Time « Date Time « C# / C Sharp






illustrates the use of TimeSpan properties and methods

illustrates the use of TimeSpan properties and methods
    
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example9_5.cs illustrates the use of TimeSpan properties and methods
*/

using System;

public class Example9_5
{

  public static void DisplayTimeSpan(
    string name, TimeSpan myTimeSpan
  )
  {

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

    // display the TimeSpan's properties
    Console.WriteLine(name + ".Days = " + myTimeSpan.Days);
    Console.WriteLine(name + ".Hours = " + myTimeSpan.Hours);
    Console.WriteLine(name + ".Minutes = " + myTimeSpan.Minutes);
    Console.WriteLine(name + ".Seconds = " + myTimeSpan.Seconds);
    Console.WriteLine(name + ".Milliseconds = " +
      myTimeSpan.Milliseconds);
    Console.WriteLine(name + ".Ticks = " + myTimeSpan.Ticks);

  }


  public static void Main()
  {

    // create a TimeSpan instance, specifying the hours, minutes,
    // and seconds
    int hours = 4;
    int minutes = 12;
    int seconds = 10;
    TimeSpan myTimeSpan = new TimeSpan(hours, minutes, seconds);
    Console.WriteLine("myTimeSpan = " + myTimeSpan);

    // create a TimeSpan instance, specifying the days, hours, minutes,
    // and seconds
    int days = 1;
    TimeSpan myTimeSpan2 = new TimeSpan(days, hours, minutes, seconds);
    Console.WriteLine("myTimeSpan2 = " + myTimeSpan2);

    // create a TimeSpan instance, specifying the days, hours, minutes,
    // seconds, and milliseconds
    int milliseconds = 20;
    TimeSpan myTimeSpan3 =
      new TimeSpan(days, hours, minutes, seconds, milliseconds);
    Console.WriteLine("myTimeSpan3 = " + myTimeSpan3);

    // create a TimeSpan instance, specifying the number of ticks
    long ticks = 300;
    TimeSpan myTimeSpan4 = new TimeSpan(ticks);
    Console.WriteLine("myTimeSpan4 = " + myTimeSpan4);

    // display the properties for myTimeSpan
    Console.WriteLine("myTimeSpan.Days = " +
      myTimeSpan.Days);
    Console.WriteLine("myTimeSpan.Hours = " +
      myTimeSpan.Hours);
    Console.WriteLine("myTimeSpan.Minutes = " +
      myTimeSpan.Minutes);
    Console.WriteLine("myTimeSpan.Seconds = " +
      myTimeSpan.Seconds);
    Console.WriteLine("myTimeSpan.Milliseconds = " +
      myTimeSpan.Milliseconds);
    Console.WriteLine("myTimeSpan.Ticks = " +
      myTimeSpan.Ticks);
    Console.WriteLine("myTimeSpan.TotalDays = " +
      myTimeSpan.TotalDays);
    Console.WriteLine("myTimeSpan.TotalHours = " +
      myTimeSpan.TotalHours);
    Console.WriteLine("myTimeSpan.TotalMinutes = " +
      myTimeSpan.TotalMinutes);
    Console.WriteLine("myTimeSpan.TotalSeconds = " +
      myTimeSpan.TotalSeconds);
    Console.WriteLine("myTimeSpan.TotalMilliseconds = " +
      myTimeSpan.TotalMilliseconds);

    // use the FromDays(), FromHours(), FromMinutes(), FromSeconds(),
    // FromMilliseconds(), and FromTicks() methods to create new
    // TimeSpan instances
    TimeSpan myTimeSpan5 = TimeSpan.FromDays(5);
    Console.WriteLine("TimeSpan.FromDays(5) = " +
      myTimeSpan5);
    TimeSpan myTimeSpan6 = TimeSpan.FromHours(10);
    Console.WriteLine("TimeSpan.FromHours(10) = " +
      myTimeSpan6);
    TimeSpan myTimeSpan7 = TimeSpan.FromMinutes(30);
    Console.WriteLine("TimeSpan.FromMinutes(30) = " +
      myTimeSpan7);
    TimeSpan myTimeSpan8 = TimeSpan.FromSeconds(15);
    Console.WriteLine("TimeSpan.FromSeconds(15) = " +
      myTimeSpan8);
    TimeSpan myTimeSpan9 = TimeSpan.FromMilliseconds(200);
    Console.WriteLine("TimeSpan.FromMilliseconds(200) = " +
      myTimeSpan9);
    TimeSpan myTimeSpan10 = TimeSpan.FromTicks(500);
    Console.WriteLine("TimeSpan.FromTicks(500) = " +
      myTimeSpan10);

    // use the Parse() method to convert strings to TimeSpan instances
    TimeSpan myTimeSpan11 = TimeSpan.Parse("8:10:30");
    Console.WriteLine("TimeSpan.Parse(\"8:10:30\") = " +
      myTimeSpan11);
    TimeSpan myTimeSpan12 = TimeSpan.Parse("1.8:10:30.1234567");
    Console.WriteLine("TimeSpan.Parse(\"1.8:10:30.1234567\") = " +
      myTimeSpan12);

    // use the Add() method to add a TimeSpan instance to another
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14);
    Console.WriteLine("myTimeSpan13 = " + myTimeSpan13);
    Console.WriteLine("myTimeSpan14 = " + myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);

    // use the Subtract() method to subtract a TimeSpan instance
    // from another
    myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);

    // use the Duration() method to add two TimeSpan instances
    Console.WriteLine("myTimeSpan15.Duration() = " +
      myTimeSpan15.Duration());

    // use the Negate() method to add two TimeSpan instances
    Console.WriteLine("myTimeSpan15.Negate() = " +
      myTimeSpan15.Negate());
    Console.WriteLine("myTimeSpan14.Negate() = " +
      myTimeSpan14.Negate());

  }

}


           
         
    
    
    
  








Related examples in the same category

1.Current date and time
2.What day of the month is this?
3.Do some leap year checks
4.Look at the min and max date/time values
5.Output DateTime object
6.Constructors of DateTime
7.comparisons between DateTime objects
8.Parse and ParseExact
9.DateTime Now and its calculation
10.new DateTime(1900, 2, 29)
11.new DateTime(1900, 2, 29, new JulianCalendar())
12.Specify Kind DateTime
13.Offset of DateTime
14.DateTime and TimeSpan Instances
15.use the Now and UtcNow properties to get the currrent date and time
16.display the Date, Day, DayOfWeek, DayOfYear,Ticks, and TimeOfDayProperties of myDateTime
17.use the Compare() method to compare DateTime instances
18.use the overloaded less than operator (<) to compare two DateTime instances
19.use the Equals() method to compare DateTime instances
20.use the DaysInMonth() method to retrieve the number of days in a particular month and year
21.use the IsLeapYear() method to determine if a particular year is a leap year
22.use the Parse() method to convert strings to DateTime instances
23.use the Subtract() method to subtract a TimeSpan from a DateTime
24.use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime
25.Illustrates the use of DateTime and TimeSpan instancesIllustrates the use of DateTime and TimeSpan instances
26.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
27.A simple clockA simple clock
28.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
29.Change current culture and back
30.How values you passed into contructor mapped to its Properties
31.Gets the day of the week
32.Returns the number of days in the specified month and year.
33.DateTime and ==
34.Create a DateTime structure with local time
35.Compare DateTime instance with Equal methods
36.Format a DateTime with M/dd/yyyy h:mm:ss.fff tt
37.Check the Kind of DateTime
38.DateTimeFormatInfo.CurrentInfo.TimeSeparator
39.Initializes a DateTime structure to a specified number of ticks.
40.DateTime.Now, DateTime.UtcNow, DateTime.Today
41.Parse a date time value from a string with Invariant Culture
42.Default value of DateTime is DateTime.MinValue
43.Default ToString output
44.Convert DateTime to string with fr-FR culture
45.ToString single letter format: F
46.DateTime ToString single letter format with fr-FR culture
47.DateTime Compare
48.DateTime CompareTo
49.Gets the date component of this instance.
50.Time String
51.Create day and year for a certain month
52.Returns the number of seconds since January 1, 1970