using System; using System.IO; class Program { static void Main(string[] args) { DateTime startTime = DateTime.Now; // some code to execute TimeSpan elapsedTime = DateTime.Now - startTime; using (var writer = new BinaryWriter(File.Open("elapsedTime.bin", FileMode.Create))) { writer.WriteDeltaTime(elapsedTime); } } }
using System; using System.IO; class Program { static void Main(string[] args) { DateTime time1 = new DateTime(2021, 1, 1, 0, 0, 0); DateTime time2 = DateTime.Now; TimeSpan timeDifference = time2 - time1; byte[] buffer = new byte[8]; using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) { writer.WriteDeltaTime(timeDifference); stream.Seek(0, SeekOrigin.Begin); stream.Read(buffer, 0, 8); } // buffer now contains the serialized time difference } }In this example, we use WriteDeltaTime to write the difference between two dates to a memory stream, and then read the serialized data into a byte array. The package library providing the GenericWriter class is part of the .NET Framework Class Library. Specifically, the WriteDeltaTime extension method is defined in the System.IO.BinaryWriterExtensions class in the System.IO namespace.