Csharp/CSharp Tutorial/Date Time/TimeSpan
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
- 1 Create a TimeSpan instance, specifying the days, hours, minutes,and seconds
- 2 Create a TimeSpan instance, specifying the days, hours, minutes, seconds, and milliseconds
- 3 Create a TimeSpan instance, specifying the hours, minutes, and seconds
- 4 Create a TimeSpan instance, specifying the number of ticks
- 5 Create a TimeSpan representing 1 week
- 6 Create a TimeSpan representing 2.5 days
- 7 Display the properties for myTimeSpan
- 8 Manual Profiler
- 9 Measures the time taken to add some numbers
- 10 TimeSpan calculation
- 11 Use the Add() method to add a TimeSpan instance to another
- 12 Use the Add() method to add a TimeSpan to a DateTime
- 13 Use the Duration() method to add two TimeSpan instances
- 14 Use the FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds(), and FromTicks() methods to create new TimeSpan instances
- 15 Use the Negate() method to add two TimeSpan instances
- 16 Use the Subtract() method to subtract a TimeSpan from a DateTime
- 17 Use the Subtract() method to subtract a TimeSpan instance from another
Create a TimeSpan instance, specifying the days, hours, minutes,and seconds
using System;
class MainClas
{
public static void Main()
{
int hours = 4;
int minutes = 12;
int seconds = 10;
int days = 1;
TimeSpan myTimeSpan2 = new TimeSpan(days, hours, minutes, seconds);
Console.WriteLine("myTimeSpan2 = " + myTimeSpan2);
}
}
myTimeSpan2 = 1.04:12:10
Create a TimeSpan instance, specifying the days, hours, minutes, seconds, and milliseconds
using System;
class MainClas
{
public static void Main()
{
int hours = 4;
int minutes = 12;
int seconds = 10;
int days = 1;
int milliseconds = 20;
TimeSpan myTimeSpan3 = new TimeSpan(days, hours, minutes, seconds, milliseconds);
Console.WriteLine("myTimeSpan3 = " + myTimeSpan3);
}
}
myTimeSpan3 = 1.04:12:10.0200000
Create a TimeSpan instance, specifying the hours, minutes, and seconds
using System;
class MainClas
{
public static void Main()
{
int hours = 4;
int minutes = 12;
int seconds = 10;
TimeSpan myTimeSpan = new TimeSpan(hours, minutes, seconds);
Console.WriteLine("myTimeSpan = " + myTimeSpan);
}
}
myTimeSpan = 04:12:10
Create a TimeSpan instance, specifying the number of ticks
using System;
class MainClas
{
public static void Main()
{
long ticks = 300;
TimeSpan myTimeSpan4 = new TimeSpan(ticks);
Console.WriteLine("myTimeSpan4 = " + myTimeSpan4);
}
}
myTimeSpan4 = 00:00:00.0000300
Create a TimeSpan representing 1 week
using System;
class MainClass
{
public static void Main(string[] args)
{
TimeSpan oneWeek = new TimeSpan(4, 12, 0, 0) + new TimeSpan(2, 12, 0, 0);
// Create a DateTime with the current date and time.
DateTime now = DateTime.Now;
// Create a DateTime representing 1 week in the future.
DateTime future = now + oneWeek;
Console.WriteLine("Future: {0}", future);
}
}
Future: 01/04/2007 2:10:53 PM
Create a TimeSpan representing 2.5 days
using System;
class MainClass
{
public static void Main(string[] args)
{
TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
Console.WriteLine(timespan1);
}
}
2.12:00:00
Display the properties for myTimeSpan
using System;
class MainClas
{
public static void Main()
{
long ticks = 300;
TimeSpan myTimeSpan = new TimeSpan(ticks);
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);
}
}
myTimeSpan.Days = 0 myTimeSpan.Hours = 0 myTimeSpan.Minutes = 0 myTimeSpan.Seconds = 0 myTimeSpan.Milliseconds = 0 myTimeSpan.Ticks = 300 myTimeSpan.TotalDays = 3.47222222222222E-10 myTimeSpan.TotalHours = 8.33333333333333E-09 myTimeSpan.TotalMinutes = 5E-07 myTimeSpan.TotalSeconds = 3E-05 myTimeSpan.TotalMilliseconds = 0.03
Manual Profiler
using System;
using System.Diagnostics;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
DateTime Start = DateTime.Now;
DateTime End = DateTime.Now;
TimeSpan CallTime = End - Start;
Console.WriteLine("Call Time(MS): " + CallTime.Milliseconds.ToString());
}
}
Call Time(MS): 0
Measures the time taken to add some numbers
using System;
class MainClass
{
public static void Main()
{
DateTime start = DateTime.Now;
long total = 0;
for (int count = 0; count < 1000000; count++)
{
total += count;
}
TimeSpan timeTaken = DateTime.Now - start;
Console.WriteLine("Milliseconds = " + timeTaken.Milliseconds);
Console.WriteLine("total = " + total);
}
}
Milliseconds = 0 total = 499999500000
TimeSpan calculation
using System;
class MainClass
{
public static void Main(string[] args)
{
TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
// Create a TimeSpan representing 4.5 days.
TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
// Create a TimeSpan representing 1 week.
TimeSpan oneWeek = timespan1 + timespan2;
Console.WriteLine("oneWeek : {0}", oneWeek);
}
}
oneWeek : 7.00:00:00
Use the Add() method to add a TimeSpan instance to another
using System;
class MainClas
{
public static void Main()
{
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);
}
}
myTimeSpan13 = 01:10:13 myTimeSpan14 = 02:06:10 myTimeSpan15 = 03:16:23
Use the Add() method to add a TimeSpan to a DateTime
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime = DateTime.Parse("1/11/2004 23:10:30");
TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
DateTime myDateTime7 = myDateTime.Add(myTimeSpan);
Console.WriteLine("myTimeSpan = " + myTimeSpan);
Console.WriteLine("myDateTime6.Add(myTimeSpan) = " + myDateTime7);
}
}
myTimeSpan = 1.02:04:10 myDateTime6.Add(myTimeSpan) = 03/11/2004 1:14:40 AM
Use the Duration() method to add two TimeSpan instances
using System;
class MainClas
{
public static void Main()
{
TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
Console.WriteLine("myTimeSpan15.Duration() = " + myTimeSpan15.Duration());
}
}
myTimeSpan15.Duration() = 00:55:57
Use the FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds(), and FromTicks() methods to create new TimeSpan instances
using System;
class MainClas
{
public static void Main()
{
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);
}
}
TimeSpan.FromDays(5) = 5.00:00:00 TimeSpan.FromHours(10) = 10:00:00 TimeSpan.FromMinutes(30) = 00:30:00 TimeSpan.FromSeconds(15) = 00:00:15 TimeSpan.FromMilliseconds(200) = 00:00:00.2000000 TimeSpan.FromTicks(500) = 00:00:00.0000500
Use the Negate() method to add two TimeSpan instances
using System;
class MainClas
{
public static void Main()
{
TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
Console.WriteLine("myTimeSpan15.Negate() = " + myTimeSpan15.Negate());
Console.WriteLine("myTimeSpan14.Negate() = " + myTimeSpan14.Negate());
}
}
myTimeSpan15.Negate() = 00:55:57 myTimeSpan14.Negate() = -02:06:10
Use the Subtract() method to subtract a TimeSpan from a DateTime
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime = DateTime.Parse("1/11/2004 23:10:30");
TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
DateTime myDateTime7 = myDateTime.Subtract(myTimeSpan);
Console.WriteLine("myTimeSpan = " + myTimeSpan);
Console.WriteLine("myDateTime6.Subtract(myTimeSpan) = " + myDateTime7);
}
}
myTimeSpan = 1.02:04:10 myDateTime6.Subtract(myTimeSpan) = 31/10/2004 9:06:20 PM
Use the Subtract() method to subtract a TimeSpan instance from another
using System;
class MainClas
{
public static void Main()
{
TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);
}
}
myTimeSpan15 = -00:55:57