TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# DateTime Subtract Method

Use the DateTime.Subtract method. Subtract computes differences between 2 dates.
DateTime Subtract. One DateTime can be subtracted from another. This returns the difference in time between the 2 dates. For example, the difference between December 25 and January 1 in the year 2008 is seven days.
Example. We use the Subtract method with one argument on the DateTime struct instance. You can use Subtract to get the difference between 2 dates. You must subtract the smaller date from the larger date.Struct

Next: We see that the difference between Christmas (December 25) and New Year's Day (January 1) is seven days at the end of 2008.

Info: Note how the DateTime constructors are passed three arguments. These specify the year, the month, and the day.

Negative TimeSpans: If you subtract a later date from an earlier date, you can receive a negative TimeSpan value.

Note: For example, change the "newYears" parameters to be the year 2007, and you will get -724 days (negative time).

C# program that uses DateTime.Subtract using System; class Program { static void Main() { // Create DateTime instances for December 25 and January 1. // ... Then compute the difference with Subtract. // ... Write the result. DateTime christmas = new DateTime(2008, 12, 25); DateTime newYears = new DateTime(2009, 1, 1); TimeSpan span = newYears.Subtract(christmas); Console.WriteLine(span); Console.WriteLine("{0} days", span.TotalDays); } } Output 7.00:00:00 7 days
Discussion. Each DateTime struct stores a ticks field—this is a long type representation of the time. When using Subtract, the long fields from the two DateTime instances are simply subtracted.Long and ulong Types

And: This can be implemented using low-level intermediate language. Please look inside Subtract with IL Disassembler.

IL Disassembler Tutorial
Summary. We saw the DateTime type and its Subtract instance method. This method receives one argument and returns a TimeSpan instance indicating the elapsed time. Typically we subtract a smaller and earlier date from the larger date.DateTime
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf