Compare DateTimeOffset
In this chapter you will learn:
- Compare two DateTimeOffset values for equality
- How to use EqualsExact method to compare DateTimeOffset
- Compare method
Compare two DateTimeOffset values for equality
using System;/*from j a va 2s.co m*/
using System.Globalization;
public class Test
{
public static void Main()
{
DateTimeOffset firstTime = new DateTimeOffset(2010, 9, 2, 6, 45, 0, new TimeSpan(-7, 0, 0));
DateTimeOffset secondTime = firstTime;
Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
secondTime = new DateTimeOffset(2010, 9, 2, 6, 45, 0, new TimeSpan(-6, 0, 0));
Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
secondTime = new DateTimeOffset(2010, 9, 2, 8, 45, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine("{0} = {1}: {2}", firstTime, secondTime, firstTime.Equals(secondTime));
}
}
EqualsExact method
using System;//from j a v a2s . c o m
using System.Globalization;
public class Test
{
public static void Main()
{
DateTimeOffset instanceTime = new DateTimeOffset(2007, 10, 31, 0, 0, 0, DateTimeOffset.Now.Offset);
DateTimeOffset otherTime = instanceTime;
Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime, instanceTime.EqualsExact(otherTime));
otherTime = new DateTimeOffset(instanceTime.DateTime, TimeSpan.FromHours(instanceTime.Offset.Hours + 1));
Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime, instanceTime.EqualsExact(otherTime));
otherTime = new DateTimeOffset(instanceTime.DateTime + TimeSpan.FromHours(1), TimeSpan.FromHours(instanceTime.Offset.Hours + 1));
Console.WriteLine("{0} = {1}: {2}", instanceTime, otherTime,instanceTime.EqualsExact(otherTime));
}
}
Compare method
using System;//from j a v a 2s. com
public class CompareTimes
{
public static void Main()
{
DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0, new TimeSpan(-7, 0, 0));
DateTimeOffset secondTime = firstTime;
Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));
secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0, new TimeSpan(-6, 0, 0));
Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));
secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0, new TimeSpan(-5, 0, 0));
Console.WriteLine(DateTimeOffset.Compare(firstTime, secondTime));
}
}
Next chapter...
What you will learn in the next chapter:
- Create DateTimeOffset using the specified DateTime value.
- Create DateTimeOffset from ticks and offset
Home » C# Tutorial » Date, Time, TimeZone