Advertisement
ivandrofly

DateTime vs DateTimeOffset

Dec 3rd, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TestDateTimes
  4. {
  5.     using System;
  6.  
  7.     public class DateArithmetic
  8.     {
  9.         public static void Main()
  10.         {
  11.             DateTime date1, date2;
  12.             DateTimeOffset dateOffset1, dateOffset2;
  13.             TimeSpan difference;
  14.  
  15.             // Find difference between Date.Now and Date.UtcNow
  16.             date1 = DateTime.Now;
  17.             date2 = DateTime.UtcNow;
  18.             difference = date1 - date2;
  19.             Console.WriteLine("{0} - {1} = {2}", date1, date2, difference);
  20.  
  21.             // Find difference between Now and UtcNow using DateTimeOffset
  22.             dateOffset1 = DateTimeOffset.Now;
  23.             dateOffset2 = DateTimeOffset.UtcNow;
  24.             difference = dateOffset1 - dateOffset2;
  25.             Console.WriteLine("{0} - {1} = {2}", dateOffset1, dateOffset2, difference);
  26.  
  27.             // RAN IN PORTUGAL
  28.             // 12/03/19 5:31:13 PM - 12/03/19 5:31:13 PM = -00:00:00.0055231
  29.             // 12/03/19 5:31:13 PM +00:00 - 12/03/19 5:31:13 PM +00:00 = -00:00:00.0000577
  30.  
  31.             // MICROSOFT EXAMPLE
  32.             // If run in the Pacific Standard time zone on 4/2/2007, the example
  33.             // displays the following output to the console:
  34.             //    4/2/2007 7:23:57 PM - 4/3/2007 2:23:57 AM = -07:00:00
  35.             //    4/2/2007 7:23:57 PM -07:00 - 4/3/2007 2:23:57 AM +00:00 = 00:00:00                        
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement